【发布时间】:2013-06-19 20:33:32
【问题描述】:
我整天都在寻找一种在ComboBox 上显示默认文本字符串的方法,而我设法找到的最接近的方法是使用水印的示例。当我的应用程序打开时,ComboBox 的 Visibility 属性设置为 Collapsed,然后通过命令使其可见。不幸的是,我无法让水印效仿。这是我正在使用的:
<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} 无效,即使窗口中的其他控件绑定到它并正常运行。
<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}" />
以及视图模型中的Visible 属性:
public Visibility Visible
{
get { return _visibile; }
set
{
if (_visibile == value)
return;
_visibile = value;
RaisePropertyChanged(() => Visible);
}
}
我可以做些什么来使样式中的 setter 行为并注册绑定?
如果您需要其他代码,我很乐意提供。
更新: Snoop 在 TextBlock 的 Visibility 属性上显示绑定错误。在 DataContext 选项卡上,它显示“对象为空”。我一直在寻找解决此问题的方法,但我无法弄清楚如何解决。如果有人愿意将我推向正确的方向,我当然会很感激。代码来自这里http://archive.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=PierreCode&ReleaseId=3546
我不一定要寻找完整的演练,只要有足够的建议来指导我找到解决方案。
【问题讨论】:
-
使用 Snoop 检查可视树并查看该元素的
DataContext是什么。我怀疑您在 Visual Studio 输出窗口中也可能有绑定错误信息。 -
当我使用 Snoop 时,会发生这种情况:
The assembly with display name 'Snoop.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'Snoop.XmlSerializers, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.Snoop 在其他应用程序上运行良好。 -
Snoop 在
TextBlock的可见性属性上显示绑定错误。在 DataContext 选项卡上,它显示“对象为空”。 -
那是因为
TextBlock可能是由行为创建的。您必须更改 Behavior 中的代码,以便TextBlock收到与 AssociatedObject 相同的 DataContext。 -
嗯..这应该很有趣。