First, sorry for my english.
I have a problem in a project I'm working on and I can't put the finger on what is wrong.
I would really appreciate if someone can help me or give me an advice to solve this issue.
I could create a minimal reproduction case to better explain my issue:
让我们创建一个名为Foo的类:
public class Foo
{
public Foo(byte _id, string _name)
{
ID = _id;
Name = _name;
}
public byte ID { get; set; }
public string Name { get; set; }
public bool Option1 { get; set; }
public bool Option2 { get; set; }
public bool Option3 { get; set; }
}
然后在MainWindow中,让我们创建一个名为‘foos’的‘ObservableCollection’,并用一些值填充它:
public partial class MainWindow : Window
{
public ObservableCollection<Foo> Foos { get; set; } = new ObservableCollection<Foo>();
public MainWindow()
{
InitializeComponent();
DataContext = this;
Foos.Add(new Foo(1, "foo 1") { Option1 = true });
Foos.Add(new Foo(2, "foo 2") { Option2 = true });
Foos.Add(new Foo(3, "foo 3") { Option3 = true });
}
}
In the UI, I have now a datagrid binded to Foos displaying some elements (Name & ID).
On the right side I have some RadioButton linked to the 'SelectedItem' of the datagrid displaying the Option 1,2&3:
<StackPanel Orientation="Horizontal">
<DataGrid x:Name="DtgSource" ItemsSource="{Binding Foos}"
AutoGenerateColumns="False"
CanUserAddRows="False"
Width="100"
SelectionChanged="DtgSource_SelectionChanged"
SelectedValuePath="Name">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding ID}" IsReadOnly="True"/>
<DataGridTextColumn Binding="{Binding Name}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Vertical" >
<RadioButton GroupName="Options" IsChecked="{Binding ElementName=DtgSource,
Path=SelectedItem.Option1}" Content="Option 1"/>
<RadioButton GroupName="Options" IsChecked="{Binding ElementName=DtgSource,
Path=SelectedItem.Option2}" Content="Option 2"/>
<RadioButton GroupName="Options" IsChecked="{Binding ElementName=DtgSource,
Path=SelectedItem.Option3}" Content="Option 3"/>
</StackPanel>
</StackPanel>
有时,当我在数据网格中 Select 一个对象时,即使我从未点击过任何单选按钮,选项也会发生变化: 我有这样的行为:
- Select foo 1:单选按钮状态:op1:True|op2:False|op3:False
- Select foo 2:单选按钮状态:op1:False|op2:True|op3:False
- Select foo 3:单选按钮状态:op1:False|op2:False|op3:True
- Select foo 1:单选按钮状态:op1:True|op2:False|op3:False
- Select FOO 3:单选按钮状态:OP1:FALSE|OP2:FALSE|Op3:False
我是否遗漏了什么,或者有没有更好的实现来避免这种行为? 在我正在工作的项目中,我有除单选按钮之外的其他控件,我只使用RadioButton来面对这个问题.
我try 使用 Select 面板的其他控件,如ListView和ListBox.->;相同的行为
I tried also to implement Foo differently to have more information to debug and to catch the moment the options are modified:
SelectionChanged method:
private void DtgSource_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var d = sender as DataGrid;
Foo selitem = d.SelectedItem as Foo;
Console.WriteLine($"SelectionChanged to {d.SelectedValue}| options -> {selitem.Option1}|{selitem.Option2}|{selitem.Option3}");
}
用于调试的选项实现:
public class Foo
{
private bool option1;
public bool Option1 { get => option1; set { if (option1 != value) { option1 = value; Console.WriteLine($"{Name}|Option1 set to {value}"); } } }
//...
如上所述,控制台中的输出如下:
SelectionChanged to foo 1| options -> True|False|False
SelectionChanged to foo 2| options -> False|True|False
SelectionChanged to foo 3| options -> False|False|True
foo 3|Option3 set to False
SelectionChanged to foo 1| options -> True|False|False
SelectionChanged to foo 3| options -> False|False|False
通过在任何选项的"set"上插入断点并查看调用堆栈,我看到调用是由"外部代码"完成的-&>用户界面
/!\在问这个问题之前,我可以找到这个:wpf RadioButton Binding Issue 这似乎是有关联的.但公认的答案是避免使用‘GroupName’属性.这是我试过的东西,遗憾的是,行为完全相同.
如我所说,如能提供任何帮助,请理解这里正在发生的事情:)