Binding Setter Property Value in WPF

I spent all day searching for a way to display a line of text by default on a ComboBox , and the closest I found that worked, was an example that uses watermarks. When my application opens, the ComboBox Visibility property is set to "Difficult" and then becomes a visible command. Unfortunately, I cannot get a watermark to follow suit. Here is what I work with:

 <Style x:Key="watermarkLabelStyle"> <Setter Property="TextBlock.Foreground" Value="Black" /> <Setter Property="FrameworkElement.Opacity" Value="0.8" /> <Setter Property="TextBlock.FontSize" Value="12" /> <Setter Property="TextBlock.FontStyle" Value="Italic" /> <Setter Property="TextBlock.Margin" Value="8,4,4,4" /> <Setter Property="TextBlock.Visibility" Value="{Binding Visible}" /> </Style> 

{Binding Visible} does not work, although other controls in the window are bound to it and behave correctly.

 <ComboBox ItemsSource="{Binding LeagueFormatsNode}" x:Name="leagueFormatComboBox" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="3" ScrollViewer.CanContentScroll="False" HorizontalContentAlignment="Stretch" Visibility="{Binding Visible}" Behaviors:WatermarkComboBoxBehavior.EnableWatermark="True" Behaviors:WatermarkComboBoxBehavior.Label="Select League Format" Behaviors:WatermarkComboBoxBehavior.LabelStyle="{StaticResource watermarkLabelStyle}" /> 

And the Visible property in the viewmodel:

 public Visibility Visible { get { return _visibile; } set { if (_visibile == value) return; _visibile = value; RaisePropertyChanged(() => Visible); } } 

What can I do to make the style setter behave and register the binding?

If you need additional code, I gladly provided it.


Update: Snoop displays a TextBlock Visibility property binding error. On the DataContext tab, it indicates that "the object is null." I was looking for a way to fix this, but I could not figure out how to do this. If someone were kind enough to push me in the right direction, I would certainly appreciate him. The code appeared here http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PierreCode&ReleaseId=3546

I'm not necessarily looking for a complete step-by-step guide, enough advice to help me in my decision.

+6
source share
1 answer

Based on your published code, I assume you are using Behavior from here

Now, if you download the zip sample to the link above, you will get 5 files that will provide you with this Behavior set (found in the Behavior folder).

Edit TextBlockAdorner.cs

In the constructor immediately after the line

 m_TextBlock = new TextBlock { Style = labelStyle, Text = label }; 

add

 m_TextBlock.DataContext = adornedElement; 

Now in the Style setting, set your Binding to

 <Setter Property="TextBlock.Visibility" Value="{Binding DataContext.Visible}" /> 

and you have to do.

Side Notes:

  • Do not keep System.Windows.Visibility in your virtual machine. Store the Visibility property in VM as a bool , and when your Binding in BooleanToVisibilityConverter uses the BooleanToVisibilityConverter (available directly in xaml. You do not need to create one)
  • When your definition of Style gets in the habit of specifying Type="..." . This not only helps to determine at a glance that Style is related to what, but also preserves some redundant type qualifications for each of your setter properties.

so something like

 <Setter Property="FrameworkElement.Opacity" Value="0.8" /> 

will be

 <Style x:Key="watermarkLabelStyle" TargetType="{x:Type TextBlock}"> ... <Setter Property="Opacity" Value="0.8" /> 
  • Finally, I hope this is just a typo in your code, but if you are not trying to fulfill any naming convention with your Properties. In your virtual machine, your property is called Visible , and the private back-end is _visibile .
+6
source

Source: https://habr.com/ru/post/947688/


All Articles