简体   繁体   English

将BooleanToVisibilityConverter绑定到视图模型中的枚举

[英]Binding BooleanToVisibilityConverter to an enum in a View Model

I a View Model, I have: 我是一个视图模型,我有:

   public enum EventViewMode
   {
       View,
       Update,
       Insert
   }

Then, in the view some controls must be visible or not accordiong a value of EventViewMode . 然后,在视图中某些控件必须可见或不符合EventViewMode的值。

For boolean values, I used BooleanToVisibilityConverter converter. 对于布尔值,我使用了BooleanToVisibilityConverter转换器。

Is there a way to use an expression in the binding system to convert the enum value to a boolean expression. 有没有一种方法可以在绑定系统中使用表达式将枚举值转换为布尔表达式。

I mean, something like: 我的意思是,类似:

<Button x:Name="btnSave"  
   Visibility="{Binding MyVariable == EventViewMode.View ,  Converter={StaticResource booleanToVisibilityConverter}}" />

Note that MyVariable == EventViewMode.View does not work, it is for showing what I want to reach. 请注意, MyVariable == EventViewMode.View不起作用,它用于显示我想要的内容。

No there isn't I'm afraid. 不,我不害怕。 Your options are 您的选择是

  • Use a value converter 使用值转换器
  • Expose the property as a visbility instead of a EventViewMode 将属性公开为可见性,而不是EventViewMode
  • Use data triggers to set the desired property (in this case visibility) based on Enum values, for example 例如,使用数据触发器基于Enum值设置所需的属性(在这种情况下为可见性)

     <Button x:Name="btnSave" Visibility="{Binding MyVariable == EventViewMode.View , Converter={StaticResource booleanToVisibilityConverter}}"> <Button.Style> <Style TargetType={x:Type Button}> <Setter Property="Visibility" Value="Collapsed" /> <Style.Triggers> <DataTrigger Binding="{Binding MyVariable}" Value="View"> <Setter Property="Visibility" Value="Visible" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> 

You could simply expose a property in your view model: 您可以简单地在视图模型中公开一个属性:

public bool IsVisible { get { return MyVariable == EventViewMode.View; } }

Don't forget to raise property changed notifications for IsVisible when you change MyVariable. 更改MyVariable时,不要忘记引发IsVisible的属性更改通知。

If you don't want to pollute your view model, the next simplest option is to implement another value converter. 如果您不想污染您的视图模型,那么下一个最简单的选择就是实现另一个值转换器。

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

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