我正在编写一个带有 ListView 的 UWP 应用程序。 ListView 项包含一个文本 block 和一个复选框。当选择 ListView 项时,我希望选中/取消选中该复选框。我还想删除“选定”动画,其中 ListView 项在被选择时会变成蓝色。
我找到了不同的解决方案,但它们似乎都依赖于触发器的使用,而 Visual Studio 告诉我触发器在 UWP 中不可用。
在 UWP 中没有触发器的情况下如何解决这个问题?
我的 ListView :
<ListView Name="ListViewItems" Grid.Row="2">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Margin" Value="0,0,0,1"></Setter>
<Setter Property="Background" Value="White"></Setter>
<Setter Property="Padding" Value="5"></Setter>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Padding="5,0">
<CheckBox HorizontalAlignment="Right" VerticalAlignment="Center" Name="CheckBoxItem"></CheckBox>
<TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Name="TextblockItem" Text="{Binding}"></TextBlock>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
最佳答案
When the listviewitem is selected, I would like the checkbox to check/uncheck.
您可以直接将 CheckBox 的 IsChecked 属性绑定(bind)到 ListViewItem IsSelected 属性:
<CheckBox
HorizontalAlignment="Right"
Margin="10"
VerticalAlignment="Center"
IsChecked="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListViewItem}}}"
Name="CheckBoxItem">
</CheckBox>
每当ListViewItem的IsSelected属性发生变化时,CheckBox就会被选中/取消选中。
I would also like to remove the "selected" animation, where the listviewitem turns blue when it is selected.
下面的代码可以帮助您实现此目的,但是,它会覆盖项目的模板,这意味着您必须编写自己的模板。
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
关于c# - 在 ListView UWP 中选择项目时选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669637/