在WPF中绑定Setter属性的值 [英] Binding the value of a Setter Property in WPF
问题描述
我整天都在寻找一种方法来在 ComboBox
上显示默认文本字符串,而我设法找到的最接近的示例就是使用水印的示例.当我的应用程序打开时, ComboBox
的Visibility属性设置为Collapsed,然后通过命令使其可见.不幸的是,我没法跟上水印.这是我正在使用的东西:
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. When my application opens, the ComboBox
's Visibility property is set to Collapsed and then made visible by a command. 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}
无效,即使窗口中的其他控件已绑定并正常运行.
{Binding Visible}
has no effect even though other controls in the window are bound to it and behave properly.
<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}" />
以及viewmodel中的 Visible
属性:
And the Visible
property in the viewmodel:
public Visibility Visible
{
get { return _visibile; }
set
{
if (_visibile == value)
return;
_visibile = value;
RaisePropertyChanged(() => Visible);
}
}
我该怎么做才能使样式中的setter表现出来并注册绑定?
What can I do to make the setter in the style behave and register the binding?
如果您需要其他代码,我会很乐意提供.
If you need additional code, I'll gladly provide it.
更新:Snoop在TextBlock的Visibility属性上显示绑定错误.在DataContext选项卡上,它说对象为空".我一直在寻找解决此问题的方法,但我一直无法弄清楚该怎么做.如果有人愿意把我推向正确的方向,我当然会很感激.代码来自此处 http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PierreCode&ReleaseId=3546
Update: Snoop is showing a binding error on the TextBlock's Visibility property. On the DataContext tab, it says "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
我并不一定要寻求完整的演练,只是足够的指导以指导我解决问题.
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
现在,如果您在上述链接中下载示例zip,则将获得5个文件,这些文件为您提供了这组 Behavior
(位于 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).
编辑 TextBlockAdorner.cs
在该行之后的构造函数中
In the constructor just after the line
m_TextBlock = new TextBlock { Style = labelStyle, Text = label };
添加
m_TextBlock.DataContext = adornedElement;
现在在您的 Style
设置器中,将您的 Binding
切换为
Now in your Style
setter switch your Binding
to
<Setter Property="TextBlock.Visibility"
Value="{Binding DataContext.Visible}" />
您应该完成了.
注释:
- 请勿在VM中保留
System.Windows.Visibility
.将VM中的Visibility
属性保留为bool
,并且在xaml中将Binding
保留时,使用BooleanToVisibilityConverter
(直接提供)在xaml中.您不必创建一个) - 当您定义
Style
时,习惯于指定Type ="..."
.它不仅可以帮助您一目了然地确定与样式有关的Style
,而且还可以为您的每个setter属性节省一些多余的类型限定.
- Do not hold
System.Windows.Visibility
in your VM. KeepVisibility
property in the VM as abool
and when yourBinding
it in xaml use aBooleanToVisibilityConverter
(available directly in xaml. You do not have to create one) - When your defining
Style
's get into the habit of specifying aType="..."
. It not only helps identify at a glance whichStyle
's relate to what but also saves some redundant type qualification for each of your setter properties.
类似
<Setter Property="FrameworkElement.Opacity"
Value="0.8" />
将是
<Style x:Key="watermarkLabelStyle"
TargetType="{x:Type TextBlock}">
...
<Setter Property="Opacity"
Value="0.8" />
- 最后希望这只是您代码中的错字,但如果不是,请尝试遵循一些有关属性的命名约定.在您的VM中,您的媒体资源称为
Visible
,而其私有后端为_visibile
. - 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
.
这篇关于在WPF中绑定Setter属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!