How to compare 2 string using data triggers in wpf

4.9k Views Asked by At

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

3

There are 3 best solutions below

0
On BEST ANSWER

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>
0
On

Sounds to me like this is business logic, in which case, this should be handled in the Model:

public bool AreTheSame
{
    get { return Name1 == Name2; }
}

You can then bind to AreTheSame in your DataTrigger.

0
On
<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="150">
                                                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
                                                                </Border>
                                                                <ControlTemplate.Triggers>
                                                                    <DataTrigger>
                                                                        <DataTrigger.Binding>
                                                                            <MultiBinding Converter="{StaticResource StringMatchConverter}">
                                                                                <Binding Path= "CurrentPage.Name2"/>
                                                                                <Binding Path="Name1" />
                                                                            </MultiBinding>
                                                                        </DataTrigger.Binding>
                                                                        <Setter Property="Background" TargetName="Border" Value="Yellow"></Setter>
                                                                    </DataTrigger>
                                                                </ControlTemplate.Triggers>
                                                            </ControlTemplate>
                                                        </Setter.Value>
                                                    </Setter>
                                                </Style>
                                            </Label.Style>