0
我正在使用MVVM创建一个UWP应用程序。在我的xaml视图中,我有一个3状态复选框。我的复选框是在一个ListView中,而且这个listview也有一个子列表视图,有更多的复选框 - 所以基本上是复选框的嵌套列表。我试图让我的外部列表视图中的复选框处于空状态,如果内部列表视图中的任何复选框被选中。如果所有内部选项都被选中,我希望外部复选框处于真实状态,如果没有选中,我希望它处于错误状态。我正在使用委托命令来实现这些事件处理程序。3状态复选框在mvvm中绑定,无法设置为null状态
我成功地能够得到true和false的情况下工作,但我无法获得复选框显示状态null在UI上,因为它的价值在我的ViewModel成功更改为空。
<ListView ItemsSource="{Binding ObxList, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{x:Bind IsSelected, Mode=TwoWay}" IsThreeState="True" Command="{Binding DataContext.ClickCommand, ElementName=Y}">
<TextBlock Text="{x:Bind Value}" />
</CheckBox>
<ListView ItemsSource="{Binding Children}" Visibility="{Binding visibility, Mode=TwoWay}">
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"
IsThreeState="False" Command="{Binding DataContext.ChildCommand, ElementName=Y}">
<TextBlock Text="{Binding Value}"/>
</CheckBox>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</ListView.ItemTemplate>
</ListView
这里是视图模型方法被调用在我的委托指令的代码:
public void ChildCheckboxHandler()
{
// gets all of the lowest level children
foreach (Object item in AllObjectsList.Where(t =>
{
return t.Children.Count() == 0;
}))
{
if (item.IsSelected == true)
{
var parent = item.GetParent(AllObjectsList);
if (parent.IsSelected == null || parent.IsSelected == false)
{
// checks if all children are selected
// and changes the parent to true if so
// null if only some are selected
bool allChildrenSelected = true;
foreach (var child in parent.GetChildren(AllObjectsList))
{
if (child.IsSelected == false)
{
allChildrenSelected = false;
break;
}
}
if (allChildrenSelected)
{
parent.IsSelected = true;
}
else
{
parent.IsSelected = new Nullable<Boolean>();
}
}
}
else if (item.IsSelected == false)
{
var parent = item.GetParent(AllObjectsList);
if (parent.IsSelected == null)
{
// checks if all the children are unchecked
// if they are, then sets the parent to false
// if they are not, then sets the parent to null
bool noChildrenSelected = true;
foreach (var child in parent.GetChildren(AllObjectsList))
{
if (child.IsSelected == true)
{
noChildrenSelected = false;
break;
}
}
if (noChildrenSelected)
{
parent.IsSelected = false;
}
}
else if (parent.IsSelected == true)
{
parent.IsSelected = null;
}
}
}
}