基于 DataContext 属性的触发器
假设我想根据 DataContext 中的属性值显示/隐藏元素,如何实现?
// In MainWindow.xaml.cs: DataContext of MainWindow.xaml
public int Mode { get; set; }
在 XAML 中,我想根据 Mode
显示隐藏元素。我怎样才能使下面的工作?或者实现这个的适当方法是什么?
<StackPanel>
<StackPanel.Triggers>
<Trigger Property="Mode" Value="1">
<Setter TargetName="txt1" Property="Visibility" Value="Visible" />
<Setter TargetName="txt2" Property="Visibility" Value="Collapsed" />
<Setter TargetName="txt3" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="Mode" Value="2">
<Setter TargetName="txt1" Property="Visibility" Value="Collapsed" />
<Setter TargetName="txt2" Property="Visibility" Value="Visible" />
<Setter TargetName="txt3" Property="Visibility" Value="Collapsed" />
</Trigger>
</StackPanel.Triggers>
<TextBlock Text="TextBlock 1" x:Name="txt1" />
<TextBlock Text="TextBlock 2" x:Name="txt2" />
<TextBlock Text="TextBlock 3" x:Name="txt3" />
</StackPanel>
目前,我收到的错误是“在类型“StackPanel”中找不到属性“模式”。D:\tmp\WpfApplication1\TriggersAndProperties\MainWindow.xaml”
Suppose I want to show/hide elements based on Values of Properties from DataContext, how can I acheive it?
// In MainWindow.xaml.cs: DataContext of MainWindow.xaml
public int Mode { get; set; }
In XAML, I want to show hide elements based on the Mode
. How can I make the below work? Or what is the appropriate way of implementing this?
<StackPanel>
<StackPanel.Triggers>
<Trigger Property="Mode" Value="1">
<Setter TargetName="txt1" Property="Visibility" Value="Visible" />
<Setter TargetName="txt2" Property="Visibility" Value="Collapsed" />
<Setter TargetName="txt3" Property="Visibility" Value="Visible" />
</Trigger>
<Trigger Property="Mode" Value="2">
<Setter TargetName="txt1" Property="Visibility" Value="Collapsed" />
<Setter TargetName="txt2" Property="Visibility" Value="Visible" />
<Setter TargetName="txt3" Property="Visibility" Value="Collapsed" />
</Trigger>
</StackPanel.Triggers>
<TextBlock Text="TextBlock 1" x:Name="txt1" />
<TextBlock Text="TextBlock 2" x:Name="txt2" />
<TextBlock Text="TextBlock 3" x:Name="txt3" />
</StackPanel>
Currently, the Error I am getting is "Property 'Mode' was not found in type 'StackPanel'. D:\tmp\WpfApplication1\TriggersAndProperties\MainWindow.xaml"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要可以在绑定上工作的触发器,则需要使用
DataTriggers
。问题是,DataTriggers
仅在样式和模板上可用,因此您需要像这样定义一个:另一种解决方案是使用
IValueConverter
来从转换 int Mode
为您想要的Visibility
,并将其直接应用到每个文本块的Visibility
属性。请注意,Dean Chalk 的答案仍然有效:如果您希望触发
Mode
的更改,则必须使用DependencyProperty
或实现INotifyPropertyChanged
。You need to use
DataTriggers
if you want triggers that can work on bindings. Problem is,DataTriggers
are only available on style and template so you need to define one like this:Another solution would be to use an
IValueConverter
that converts the int fromMode
to theVisibility
you want, and apply it directly to each text blockVisibility
property.Note that Dean Chalk's answer stays valid: you have to use a
DependencyProperty
or implementINotifyPropertyChanged
if you want changes onMode
to trigger.您的属性“模式”需要是依赖属性才能以这种方式使用:
Your propert 'Mode' needs to be a dependency property in order to be used this way: