Xaml 绑定中的 Switch(选择)语句?
有没有办法在 XAML 中创建条件绑定?
示例:
<Window x:Name="Me" DataContext="{Binding ElementName=Me}">
<TextBlock>
<TextBlock.Text>
<SelectBinding Binding="{Binding MyValue}">
<Case Value="Value1" Value="value is 1!">
<Case Value="Value2" Value="value is 2!">
<Case Value="Value3" Value="value is 3!">
</SelectBinding >
</TextBlock.Text>
</TextBlock>
</Window>
底线是,我想根据 Binding
的另一个值设置 TextBlock 值,该值可以是一个案例列表,其中每个案例(或多个案例)都被寻址到适当的输出/设置器。
也许我可以在我的例子中使用DataTrigger
,但我只是不知道该怎么做,因为我在这里没有使用任何DataTemplate
。
更新
在我的场景中,我有一个包含多个控件的UserControl
。 我希望根据 UserControl.DataContext 数据项中的某个属性,用户控件中的其他控件应该相应地受到影响。基本上与我上面的示例相同,只是每种情况都会导致一个 Setter
列表。
Is there a way to create a conditional binding in XAML?
Example:
<Window x:Name="Me" DataContext="{Binding ElementName=Me}">
<TextBlock>
<TextBlock.Text>
<SelectBinding Binding="{Binding MyValue}">
<Case Value="Value1" Value="value is 1!">
<Case Value="Value2" Value="value is 2!">
<Case Value="Value3" Value="value is 3!">
</SelectBinding >
</TextBlock.Text>
</TextBlock>
</Window>
Bottom line, I want to set a TextBlock value according to another value of Binding
, that can be of a list of cases where each case (or cases) is addressed to the appropriate output/setter.
Maybe I can use a DataTrigger
in my case, I just don't know exactly how I am gonna do it, since I am not using any DataTemplate
here.
Update
In my scenario, I am having a UserControl
that has several controls.
I want that according to a certain property in the UserControl.DataContext data-item, other controls in the user control should get affected accordingly. Basically same as my example above just that each case leads to a list of Setter
s.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
DataTrigger
(编辑 - 原始版本有轻微错误)
use a
DataTrigger
(EDITED - original had slight mistake)
您有多种选择...
实际上,我认为这是一种风格/设计选择 - 以上没有本质上更好或更差,它们只是适合不同的场景。
You have a number of options...
Really I'd see this as a stylistic/design choice - none of the above are inherently better or worse, they're just suited to different scenarios.
尝试使用 Josh 编写的 Switch Converter:
编辑:
这是 SwitchConverter 的代码 乔希的网站似乎已关闭 -
Try to use the Switch Converter written by Josh:
Edit:
Here is code of SwitchConverter as Josh's site seems to be down -
我根据接受的答案制作了一个简化的、更新的转换器。
它还允许进行字符串比较和设置默认大小写:
SwitchConverterCase
类:示例用法:
或内联:
I made an simplified, updated converter based on the accepted answer.
It also allows a string comparison and a default case to be set:
The
SwitchConverterCase
class:Example usage:
Or inline:
您可以按照 Dan 建议使用转换器...
然后您可以在您的 xaml 中使用它...
You could just use a converter as Dan suggested...
You would then use this from within your xaml...
此问题的另一个解决方案是使用
MultiBinding
和MultiValueConverter
,如下所示:使用以下实现将输入值视为字符串:
使用
MultiBinding
增加输入和返回值的灵活性。但是,Compare() 函数还需要进一步工作以支持其他类型,例如数字和日期。Another solution to this problem is with a
MultiBinding
and aMultiValueConverter
as follows:With the following implementation which treats the input values as strings:
Using
MultiBinding
increases the flexibility of your input and return values. However, further work is required on theCompare()
function to support other types, such as numbers and dates.