DataTrigger where value is NOT null?

后端 未结 12 584
野趣味
野趣味 2020-11-27 10:56

I know that I can make a setter that checks to see if a value is NULL and do something. Example:


  
    

        
相关标签:
12条回答
  • 2020-11-27 10:59

    Stop! No converter! I dont want to "sell" the library of this guy, but I hated the fact of doing converter everytime I wanted to compare stuff in XAML.

    So with this library : https://github.com/Alex141/CalcBinding

    you can do that [and a lot more] :

    First, In the declaration of the windows/userControl :

    <Windows....
         xmlns:conv="clr-namespace:CalcBinding;assembly=CalcBinding"
    >
    

    then, in the textblock

    <TextBlock>
          <TextBlock.Style>
              <Style.Triggers>
              <DataTrigger Binding="{conv:Binding 'MyValue==null'}" Value="false">
                 <Setter Property="Background" Value="#FF80C983"></Setter>
              </DataTrigger>
            </Style.Triggers>
          </TextBlock.Style>
        </TextBlock>
    

    The magic part is the conv:Binding 'MYValue==null'. In fact, you could set any condition you wanted [look at the doc].

    note that I am not a fan of third party. but this library is Free, and little impact (just add 2 .dll to the project).

    0 讨论(0)
  • 2020-11-27 11:00

    You can use an IValueConverter for this:

    <TextBlock>
        <TextBlock.Resources>
            <conv:IsNullConverter x:Key="isNullConverter"/>
        </TextBlock.Resources>
        <TextBlock.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SomeField, Converter={StaticResource isNullConverter}}" Value="False">
                        <Setter Property="TextBlock.Text" Value="It's NOT NULL Baby!"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
    

    Where IsNullConverter is defined elsewhere (and conv is set to reference its namespace):

    public class IsNullConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value == null);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new InvalidOperationException("IsNullConverter can only be used OneWay.");
        }
    }
    

    A more general solution would be to implement an IValueConverter that checks for equality with the ConverterParameter, so you can check against anything, and not just null.

    0 讨论(0)
  • 2020-11-27 11:00

    I'm using this to only enable a button if a listview item is selected (ie not null):

    <Style TargetType="{x:Type Button}">
        <Setter Property="IsEnabled" Value="True"/>
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=lvMyList, Path=SelectedItem}" Value="{x:Null}">
                <Setter Property="IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    
    0 讨论(0)
  • 2020-11-27 11:07

    You can use DataTrigger class in Microsoft.Expression.Interactions.dll that come with Expression Blend.

    Code Sample:

    <i:Interaction.Triggers>
        <i:DataTrigger Binding="{Binding YourProperty}" Value="{x:Null}" Comparison="NotEqual">
           <ie:ChangePropertyAction PropertyName="YourTargetPropertyName" Value="{Binding YourValue}"/>
        </i:DataTrigger
    </i:Interaction.Triggers>
    

    Using this method you can trigger against GreaterThan and LessThan too. In order to use this code you should reference two dll's:

    System.Windows.Interactivity.dll

    Microsoft.Expression.Interactions.dll

    0 讨论(0)
  • 2020-11-27 11:08

    Compare with null (As Michael Noonan said):

    <Style>
        <Style.Triggers>
           <DataTrigger Binding="{Binding SomeProperty}" Value="{x:Null}">
               <Setter Property="Visibility" Value="Collapsed" />
            </DataTrigger>
         </Style.Triggers>
    </Style>
    

    Compare with not null (without a converter):

    <Style>
        <Setter Property="Visibility" Value="Collapsed" />
        <Style.Triggers>
           <DataTrigger Binding="{Binding SomeProperty}" Value="{x:Null}">
               <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>
         </Style.Triggers>
    </Style>
    
    0 讨论(0)
  • 2020-11-27 11:11

    You can use a converter or create new property in your ViewModel like that:

    public bool CanDoIt
    {
        get
        {
            return !string.IsNullOrEmpty(SomeField);
        }
    }
    

    and use it:

    <DataTrigger Binding="{Binding SomeField}" Value="{Binding CanDoIt}">
    
    0 讨论(0)
提交回复
热议问题