gpt4 book ai didi

c# - How to group checkboxes in treeview wpf mvvm when selection range is [0,1]

转载 作者:太空宇宙 更新时间:2023-11-03 14:13:58 25 4
gpt4 key购买 nike

我使用 MVVM 在 wpf 中创建了一个 TreeView 。

它工作正常,但这里有一个问题,叶节点包含一些复选框,用户只有两个选项,要么选择一个,要么不选择。

Tree View Sample Image

那么在这里我如何限制用户最多只能选择一种冷饮。

我做了一个技巧,但它没有用,当我已经选择了一种饮料然后我选择了另一种而不是我将可观察集合中的最后一个选定值设置为 false 但它不影响 View 和选择复选框保持选中状态,尽管在集合中只有一个选项的值为真。

我不能使用单选按钮 instedof 复选框,因为用户不能选择任何选项,我不能为以上任何一个提供额外的选项。

如果有人有任何解决方案,请告诉我,我将不胜感激。

更新的问题:我想我没有以正确的方式定义我的问题,所以我在这里给出我的代码片段希望这样我就能解决我的问题......

我的 View 模型类

namespace TestViewModels
{
public class ViewModel :ViewModelBase

{

private ObservableCollection<AvailableProducts> _MyTreeViewProperty

public ObservableCollection<AvailableProducts> MyTreeViewProperty
{
get { return _MyTreeViewProperty
set { _MyTreeViewProperty value;
RaisePropertyChanged("MyTreeViewProperty");}
}

public class AvailableProducts

{

private string _BrandName;

public string BrandName
{
get { return _BrandName
set { _BrandName = value; }
}


private bool _IsExpanded;
public bool IsExpanded
{
get
{
return _IsExpanded;

}
set
{
_IsExpanded = value;

}
}

private ObservableCollection<ProductTypes> _MyProductTypes

public ObservableCollection<ProductTypes> MyProductTypes
{
get { return _MyProductTypes}
set { _MyProductTypes= value; }
}


}

public class ProductTypes
{
private string _ProductTypeName;

public string ProductTypeName
{
get { return _ProductTypeName;
set { _ProductTypeNamevalue; }
}



private ObservableCollection<ProductSubTypes> _ProdSubTypes;

public ObservableCollection<ProductSubTypes> ProdSubTypes
{
get { return _ProdSubTypes;}
set { _ProdSubTypes;= value; }
}


}


public class ProductSubTypes
{
private string _ProductSubTypeName;

public string ProductSubTypeName
{
get { return _ProductSubTypeName;
set { _ProductSubTypeName;}
}

private int _ParentID;

public int ParentID
{
get { return _ParentID;}
set { _ParentID;= value; }
}


private bool _IsAssigned;

public bool IsAssigned
{
get { return _IsAssigned; }
set
{
_IsAssigned = value;

if _ParentID;!= 0)
{
//updating data in database
//Calling and setting new collection value in property

//issue : updated collection sets in setter of MyTreeViewProperty but before calling getter
// it comes to IsAssigned getter so view doesnt get updated collection of MyTreeViewProperty

}
RaisePropertyChanged("IsAssigned");
}
}


}

查看

<Page x:Class="ShiftManagerViews.Pages.ProductTreeSelection
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
DataContext="{Binding ProductsTree, Source={StaticResource Locator}}"
mc:Ignorable="d" Width="870" Height="665"

>

<TreeView Margin="10,10,0,13" ItemsSource="{Binding MyTreeViewProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left"

VerticalAlignment="Top" Width="800" Height="Auto" MinHeight="400" MaxHeight="800">
<TreeView.ItemContainerStyle>

<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
</Style>
</TreeView.ItemContainerStyle>

<TreeView.Resources>

<HierarchicalDataTemplate DataType="{x:Type local:AvailableProducts}"
ItemsSource="{Binding MyProductTypes}">
<WrapPanel>
<Image Width="20" Height="20" Source="/ShiftManagerViews;component/Images/12.bmp"/>
<Label Content="{Binding BrandName}" FontSize="14"/>
</WrapPanel>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate DataType="{x:Type local:ProductTypes}"
ItemsSource="{Binding ProdSubTypes}">
<WrapPanel>
<Image Width="18" Height="15" Source="/ShiftManagerViews;component/Images/12.bmp"/>
<Label Content="{Binding ProductTypeName}" FontSize="13"/>
</WrapPanel>
</HierarchicalDataTemplate>
<!-- the template for showing the Leaf node's properties-->
<DataTemplate DataType="{x:Type local:ProductSubTypes}">
<StackPanel>

<CheckBox IsChecked="{Binding IsAssigned, Mode=TwoWay}" Content="{Binding ProductSubTypeName}" Height="25">

</CheckBox>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>

最佳答案

ListBox 代替 TreeView 来显示子项怎么样?您可以设置样式,使项目包含 CheckBox 以显示 IsSelected 而不是突出显示项目。

关于c# - How to group checkboxes in treeview wpf mvvm when selection range is [0,1],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7022231/

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