wpfxamldatatriggermultidatatrigger

How to compare 2 string using data triggers in wpf


I have a two properties called Name1 and Name2.

How can i check whether this two properties having same value of not using Data Triggers in XAML.

Name1 Property is in Class1 and Name2 Property is in Class2.

To be more detail,

  1. I have a class called Pages which is having property Name1. While application loading, i will create a List object and add some values to that list. In XAML, i will bind it to an ItemSource.

  2. I have an another class called CurrentPage, which is again having property called Name2.

  3. In the ItemSource.ItemTemplate, I have added a label control to show the names of all Pages.

    <ItemsControl ItemsSource="{Binding Pages}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,2,0,2"></StackPanel>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
       <DataTemplate>                                  
           <Label Content="{Binding Name1}" FontSize="15" FontFamily="Arial" FontWeight="DemiBold">
           <Label.Style>
              <Style TargetType="Label">
                  <Setter Property="Template">
                      <Setter.Value>
                         <ControlTemplate TargetType="Label">
                          <Border Name="Border"  HorizontalAlignment="Center" BorderBrush="Black" BorderThickness="1" CornerRadius="0,20,20,0" Width="100">
                          <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                           </Border>
                          <ControlTemplate.Triggers>
                          <DataTrigger Binding="{Binding CurrentPage.Name2}" Value="Name1">
                               <Setter Property="Background" TargetName="Border" Value="Yellow"></Setter>
                          </DataTrigger>
                          </ControlTemplate.Triggers>
                          </ControlTemplate>
                          </Setter.Value>
                           </Setter>
               </Style>
              </Label.Style>
            </Label>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
    

  4. I am trying to change the BG color of label if it is equal to current page content.

But i am getting error and the styles are not loading. I know i was wrong in the part of comparing Name2 and Name1..() Kindly any one help me


Solution

  • Alternatively you could use a converter to compare values using multibinding in the properties Name1 and Name2:

    class StringMatchConverter : IMultiValueConverter
    {
        public object Convert(object [] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
    
            if(values.Length < 2)
            {
                return false;
            }
    
            for (int i = 1; i < values.Length; i++)
            {
                    if (!(values[0] as string).Equals(values[i] as string))
                    {
                        return false;
                    }
            }
    
            return true;
    
        }
    
        public object[] ConvertBack(object value, Type[] targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }
    }
    

    Then you can write something in these lines:

     <MultiBinding Converter="{StaticResource StringMatchConverter}">
                            <Binding Path="Name1"/>
                            <Binding XPath="Name2" />
                        </MultiBinding>