C# 从切换按钮列表视图中选择项目
我有一个列表视图,每个项目都有一个切换按钮。我希望能够在从列表中选择项目时切换按钮,在取消选择项目时不切换按钮。它必须遵循mvvm,因此没有代码隐藏 以下是我的设置:C# 从切换按钮列表视图中选择项目,c#,.net,wpf,xaml,listview,C#,.net,Wpf,Xaml,Listview,我有一个列表视图,每个项目都有一个切换按钮。我希望能够在从列表中选择项目时切换按钮,在取消选择项目时不切换按钮。它必须遵循mvvm,因此没有代码隐藏 以下是我的设置: <ListView x:Name="stampList" ItemsSource="{Binding AllStampImages}"> <ListView.ItemTemplate>
<ListView x:Name="stampList"
ItemsSource="{Binding AllStampImages}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0,2,0,0">
<ToggleButton Width="72"
Height="72"
Command="{Binding StampSelectedCommand}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我的问题是,当我点击切换按钮时,该项目未被选中。同样,当我在切换按钮外部(仍然在listView项目的边界内)点击时,该项目被选中,但按钮未被切换
我怎样把这两者联系在一起
您可能必须将
选择器.IsSelected
属性绑定到custome属性。在这种情况下,切换按钮的属性
尝试以下方法:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Selector.IsSelected" Value="{Binding [Value of your toggle button], Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
您可能必须将
选择器.IsSelected
属性绑定到custome属性。在这种情况下,切换按钮的属性
尝试以下方法:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Selector.IsSelected" Value="{Binding [Value of your toggle button], Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
您可能必须将
选择器.IsSelected
属性绑定到custome属性。在这种情况下,切换按钮的属性
尝试以下方法:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Selector.IsSelected" Value="{Binding [Value of your toggle button], Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
您可能必须将
选择器.IsSelected
属性绑定到custome属性。在这种情况下,切换按钮的属性
尝试以下方法:
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="Selector.IsSelected" Value="{Binding [Value of your toggle button], Mode=TwoWay}" />
</Style>
</ListView.ItemContainerStyle>
最干净的方法是使用ViewModel或Model layer提供的项目集合
类行项目
{
私人布尔当选;
公选学校
{
获取{return isSelected;}
集合{isSelected=value;}
}
私有字符串标签;
公共字符串标签
{
获取{return label;}
设置{label=value;}
}
}
2.2在ListViewItem模板上
右键单击listview创建模板
在菜单中:“编辑其他模板/编辑生成的项目容器(空)”
请填写以下代码:
触发器基于Model IsSelected属性。网格名为Bd,以便在触发器中更改其背景 链接到完整的工作演示: 注意 我试图仔细回答这个问题。 但以我的拙见,我不会使用ListView,因为你可能有我忽略的好理由 我更喜欢使用ItemsControl,它是一个不允许进行选择的列表框 我会设置切换按钮,如果需要,可以使用自定义模板设置蓝色背景。
它会移除所有的触发器 , ...
最佳编码最干净的方法是使用ViewModel或Model layer提供的项目集合
类行项目
{
私人布尔当选;
公选学校
{
获取{return isSelected;}
集合{isSelected=value;}
}
私有字符串标签;
公共字符串标签
{
获取{return label;}
设置{label=value;}
}
}
2.2在ListViewItem模板上
右键单击listview创建模板
在菜单中:“编辑其他模板/编辑生成的项目容器(空)”
请填写以下代码:
触发器基于Model IsSelected属性。网格名为Bd,以便在触发器中更改其背景 链接到完整的工作演示: 注意 我试图仔细回答这个问题。 但以我的拙见,我不会使用ListView,因为你可能有我忽略的好理由 我更喜欢使用ItemsControl,它是一个不允许进行选择的列表框 我会设置切换按钮,如果需要,可以使用自定义模板设置蓝色背景。
它会移除所有的触发器 , ...
最佳编码最干净的方法是使用ViewModel或Model layer提供的项目集合
类行项目
{
私人布尔当选;
公选学校
{
获取{return isSelected;}
集合{isSelected=value;}
}
私有字符串标签;
公共字符串标签
{
获取{return label;}
集合{lab