简体   繁体   English

绑定WPF中的Setter属性的值

[英]Binding the value of a Setter Property in WPF

I have spent all day looking for a way to display a default string of text on a ComboBox and the closest I managed to find that worked was an example that uses watermarking. 我整天都在寻找一种在ComboBox上显示默认文本字符串的方法,而我设法找到的最接近的工作是使用水印的示例。 When my application opens, the ComboBox 's Visibility property is set to Collapsed and then made visible by a command. 当我的应用程序打开时, ComboBox的Visibility属性设置为Collapsed,然后通过命令显示。 Unfortunately, I can't get the watermark to follow suit. 不幸的是,我无法让水印效仿。 Here is what I'm working 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} has no effect even though other controls in the window are bound to it and behave properly. {Binding Visible}即使窗口中的其他控件绑定到它并且行为正常也没有任何效果。

<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: 和viewmodel中的Visible属性:

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

What can I do to make the setter in the style behave and register the binding? 我该怎么做才能使样式中的setter表现并注册绑定?

If you need additional code, I'll gladly provide it. 如果您需要其他代码,我很乐意提供。


Update: Snoop is showing a binding error on the TextBlock's Visibility property. 更新: Snoop在TextBlock的Visibility属性上显示绑定错误。 On the DataContext tab, it says "object is null". 在DataContext选项卡上,它显示“object is null”。 I have been looking for a way to fix this but I haven't been able to figure out how. 我一直在寻找一种方法来解决这个问题,但我一直无法弄清楚如何解决这个问题。 If anybody would be kind enough to push me in the right direction, I would certainly appreciate it. 如果有人能够把我推向正确的方向,我当然会很感激。 The code came from here http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PierreCode&ReleaseId=3546 代码来自这里http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PierreCode&ReleaseId=3546

I'm not necessarily looking for a complete walkthrough, just enough advice to guide me to the solution. 我不一定要寻找完整的演练,只需要足够的建议来指导我解决方案。

Based on your posted code I'm assuming your using the Behavior from Here 根据您发布的代码,我假设您使用的是此处Behavior

Now if you download the sample zip in the above Link, you got 5 files that give you this set of Behavior 's(found in the Behavior folder). 现在,如果您在上面的链接中下载示例zip,您将获得5个文件,这些文件为您提供了这组Behavior (在Behavior文件夹中找到)。

Edit TextBlockAdorner.cs 编辑TextBlockAdorner.cs

In the constructor just after the line 在刚刚行之后的构造函数中

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

add

m_TextBlock.DataContext = adornedElement;

Now in your Style setter switch your Binding to 现在在你的Style setter中切换你的Binding

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

and you should be done. 你应该完成。

Side-Notes: 边注:

  • Do not hold System.Windows.Visibility in your VM. 不要在VM中保存System.Windows.Visibility Keep Visibility property in the VM as a bool and when your Binding it in xaml use a BooleanToVisibilityConverter (available directly in xaml. You do not have to create one) 将VM中的Visibility属性保留为bool ,当您在xaml中Binding它时使用BooleanToVisibilityConverter (直接在xaml中可用。您不必创建一个)
  • When your defining Style 's get into the habit of specifying a Type="..." . 当你定义的Style养成了指定Type="..."的习惯时。 It not only helps identify at a glance which Style 's relate to what but also saves some redundant type qualification for each of your setter properties. 它不仅有助于一目了然地确定哪种Style与哪种Style相关,而且还可以为每个特定的属性保存一些冗余类型限定。

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 hopefully this is just a typo in your code but if not try to follow some naming convention with your Properties. 最后希望这只是代码中的一个拼写错误,但如果没有尝试遵循您的属性的一些命名约定。 In your VM, your property is called Visible while it's private back-end is _visibile . 在您的VM中,您的属性称为Visible而它的私有后端是_visibile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM