DataTrigger where value is NOT null?

后端 未结 12 585
野趣味
野趣味 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 11:12

    I ran into a similar limitation with DataTriggers, and it would seem that you can only check for equality. The closest thing I've seen that might help you is a technique for doing other types of comparisons other than equality.

    This blog post describes how to do comparisons such as LT, GT, etc in a DataTrigger.

    This limitation of the DataTrigger can be worked around to some extent by using a Converter to massage the data into a special value you can then compare against, as suggested in Robert Macnee's answer.

    0 讨论(0)
  • 2020-11-27 11:12
    <StackPanel.Style>
      <Style>
        <Setter Property="StackPanel.Visibility" Value="Visible"></Setter>
        <Style.Triggers>
          <DataTrigger  Binding="{Binding ElementName=ProfileSelectorComboBox, Path=SelectedItem.Tag}" Value="{x:Null}">
              <Setter Property="StackPanel.Visibility" Value="Collapsed"></Setter>
          </DataTrigger>
        </Style.Triggers>
      </Style>
    </StackPanel.Style>
    

    I just used the inverse logic here...setting my stackpanel to invisible when my comboitem is not populated, it works pretty well!

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

    Converter:

    public class NullableToVisibilityConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? Visibility.Collapsed : Visibility.Visible;
        }
    }
    

    Binding:

    Visibility="{Binding PropertyToBind, Converter={StaticResource nullableToVisibilityConverter}}"
    
    0 讨论(0)
  • 2020-11-27 11:13

    If you are looking for a solution that does not use IValueConverter, you can always go with below mechanism

           <StackPanel>
                <TextBlock Text="Border = Red when null value" />
                <Border x:Name="border_objectForNullValueTrigger" HorizontalAlignment="Stretch" Height="20"> 
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="Black" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ObjectForNullValueTrigger}" Value="{x:Null}">
                                    <Setter Property="Background" Value="Red" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                <TextBlock Text="Border = Green when not null value" />
                <Border HorizontalAlignment="Stretch" Height="20">
                    <Border.Style>
                        <Style TargetType="Border">
                            <Setter Property="Background" Value="Green" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Background, ElementName=border_objectForNullValueTrigger}" Value="Red">
                                    <Setter Property="Background" Value="Black" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Border.Style>
                </Border>
                <Button Content="Invert Object state" Click="Button_Click_1"/>
            </StackPanel>
    
    0 讨论(0)
  • 2020-11-27 11:18

    My solution is in the DataContext instance (or ViewModel if using MVVM). I add a property that returns true if the Not Null condition I want is met.

        Public ReadOnly Property IsSomeFieldNull() As Boolean
            Get
                Return If(SomeField is Null, True, False)
            End Get
        End Property
    

    and bind the DataTrigger to the above property. Note: In VB.NET be sure to use the operator If and NOT the IIf function, which doesn't work with Null objects. Then the XAML is:

        <DataTrigger Binding="{Binding IsSomeFieldNull}" Value="False">
          <Setter Property="TextBlock.Text" Value="It's NOT NULL Baby!" />
        </DataTrigger>
    
    0 讨论(0)
  • 2020-11-27 11:20

    This is a bit of a cheat but I just set a default style and then overrode it using a DataTrigger if the value is null...

      <Style> 
          <!-- Highlight for Reviewed (Default) -->
          <Setter Property="Control.Background" Value="PaleGreen" /> 
          <Style.Triggers>
            <!-- Highlight for Not Reviewed -->
            <DataTrigger Binding="{Binding Path=REVIEWEDBY}" Value="{x:Null}">
              <Setter Property="Control.Background" Value="LightIndianRed" />
            </DataTrigger>
          </Style.Triggers>
      </Style>
    
    0 讨论(0)
提交回复
热议问题