zohaib khan

zohaib khan

  • NA
  • 26
  • 26k

WPF XMAL trigger from button to change combo box item color.

Jul 11 2012 9:10 AM

Hi,

I have combo box and button in my project(WPF application)

In following example I have change color of button when "ButtonName" press and other example I have change color of "Item3 and Item4" when selected.

Any idea how to change Background color of "Item3 and Item4" in combo box when "ButtonName" is press.?

[1]. Example1

<Button Width="73"  Height="23" Content="ButtonName" Grid.Row="1" Grid.Column="1" Margin="104,202,180,45">
  <Button.Template>
    <ControlTemplate TargetType="{x:Type Button}">
      <Border x:Name="button3" CornerRadius="2" BorderThickness="1">
        <ContentPresenter Margin="4" VerticalAlignment="Top" HorizontalAlignment="Right"/>
      </Border>
      <ControlTemplate.Triggers>
        <Trigger Property="IsPressed" Value="True">
          <Setter TargetName="button3" Property="Background" Value="Pink"/>
        </Trigger>
      </ControlTemplate.Triggers>
    </ControlTemplate>
  </Button.Template>
</Button>
 
 [2]. Example2
  
<ComboBox TabIndex="1" x:Name="txtTitle" Margin="253,40,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Height="23" SelectionChanged="txtTitle_SelectionChanged" Grid.ColumnSpan="2">
  <ComboBoxItem Content="Item1" />
  <ComboBoxItem Content="Item2" />
  <ComboBoxItem Content="Item3"  />
  <ComboBoxItem Content="Item4"  />
  <ComboBoxItem Content="Item5" />

  <ComboBox.Style>
    <Style TargetType="ComboBox">
      <Style.Triggers>
        <DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=txtTitle}" Value="Item3">
          <Setter Property="Background" Value="Pink"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=SelectedItem.Content, ElementName=txtTitle}" Value="Item4">
          <Setter Property="Background" Value="Yellow"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ComboBox.Style>
</ComboBox>

- Thanks
Zohaib.

Answers (2)