gpt4 book ai didi

c# - 在 ListView UWP 中选中项目时选中复选框

转载 作者:行者123 更新时间:2023-11-30 23:33:56 24 4
gpt4 key购买 nike

我正在编写一个带有 ListView 的 UWP 应用程序。 listviewitems 包含一个文本 block 和一个复选框。选择 listviewitem 时,我希望复选框选中/取消选中。我还想删除“选中”动画,其中 listviewitem 在被选中时变为蓝色。

我找到了不同的解决方案,但它们似乎都依赖于触发器的使用,而 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.

您可以直接将 IsChecked 属性绑定(bind)到 CheckBox 到 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.

下面的代码可以帮助您实现这一点,但是,它会覆盖 Item 的模板,这意味着您必须编写自己的模板。

            <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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com