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;
}
}
}
}