简体   繁体   中英

In a Listbox change the color of textbox when checking a checkbox c# wpf

I have a listbox with 3 buttons, 2 textboxes and a checkbox in each element.

I would like for the buttons and txtboxes to change properties(Color/or other) when i check the checkbox.

Hope someone can help me:)

Thankyou for your answer...

        <!-- LISTBOX -->
        <ListBox Name="lstBoxInfoWindow" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Foreground="Black" ScrollViewer.VerticalScrollBarVisibility="Visible" Margin="5,1.2,4.6,-0.4" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold"/>
                            <Setter Property="Background" Value="Transparent"/>
                            <Setter Property="Foreground" Value="Black"/>
                        </Trigger>
                        <Trigger Property=""
                    </Style.Triggers>
                </Style>
            </ListBox.ItemContainerStyle>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                        <Button Name="btnPackageVersion" Content ="Package version" Click="btnPackageVersion_Click" HorizontalAlignment="Right" Margin="10,0,10,0">

                        </Button>
                        <Button Name="btnFilterPath" Content ="Filter path" Click="btnFilterpath_Click" HorizontalAlignment="Right"/>
                        <TextBlock Name="txtBlkPath" Text="{Binding path, Mode=TwoWay}" FontSize="10" Width="500" Height="20" Margin="10,0,10,0"/>

                        <Button Name="btnFilterfile" Content="Filter file" Click="btnFilterfile_Click" HorizontalAlignment="Right"/>
                        <TextBlock Name="txtBlkFile" Text="{Binding file, Mode=TwoWay}" FontSize="10" Width="150" Height="20" Margin="10,0,10,0"/>

                        <CheckBox Name="chkBxKill" Content="Kill" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding keepKill.kill, Mode=TwoWay}" Margin="5,0,5,0" Checked="chkBxKill_Checked"/>
                    </StackPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

    </Grid>
</Window>

Use a DataTrigger defined in the DataTemplate .

The following example sets the Backgound of the button to red when the CheckBox is checked:

<DataTemplate>
  <StackPanel Orientation="Horizontal"
              HorizontalAlignment="Right">
    ...

    <Button Name="btnFilterfile" />    
    <CheckBox Name="chkBxKill" />
  </StackPanel>

  <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding ElementName=chkBxKill, Path=IsChecked}" Value="True">
      <Setter TargetName="btnFilterfile" Property="Background" Value="Red" />
    </DataTrigger>
  </DataTemplate.Triggers>
</DataTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM