问题描述
我有一个列表框,每个元素中有 3 个按钮、2 个文本框和一个复选框。
当我选中复选框时,我希望按钮和 txtBoxes 更改属性(颜色/或其他)。
希望有人能帮助我:)
谢谢你的回答...
<!-- 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,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"/>
<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"/>
<CheckBox Name="chkBxKill" Content="Kill" VerticalAlignment="Center" HorizontalAlignment="Right" IsChecked="{Binding keepKill.kill,Mode=TwoWay}" Margin="5,5,0" Checked="chkBxKill_Checked"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</Grid>
</Window>
解决方法
使用在 DataTrigger
中定义的 DataTemplate
。
以下示例在选中 Backgound
时将按钮的 CheckBox
设置为红色:
<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>