问题
Consider the below simplified application, which allows you to select between two elements, each of which has A and B boolean properties that are bound to radio buttons. The idea is that you should be able to set A/B independently for each element and if you switch between elements, the radio buttons should reflect the flags for that element. This isn't what happens however.
ViewModel:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
namespace SandboxWpf
{
public sealed class Element : INotifyPropertyChanged
{
private bool _memberA;
public bool MemberA
{
get { return _memberA; }
set
{
if (value != _memberA)
{
_memberA = value;
OnPropertyChanged();
}
}
}
private bool _memberB;
public bool MemberB
{
get { return _memberB; }
set
{
if (value != _memberB)
{
_memberB = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public sealed class MainViewModel : INotifyPropertyChanged
{
private readonly List<Element> _elements;
public MainViewModel()
{
_elements = new List<Element>();
_elements.Add(new Element());
_elements.Add(new Element());
}
public IEnumerable<Element> Elements
{
get { return _elements; }
}
private Element _selectedElement;
public Element SelectedElement
{
get { return _selectedElement; }
set
{
if (value != _selectedElement)
{
_selectedElement = value;
OnPropertyChanged();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Window XAML (no code behind):
<Window x:Class="SandboxWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:SandboxWpf="clr-namespace:SandboxWpf"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<SandboxWpf:MainViewModel/>
</Window.DataContext>
<StackPanel Orientation="Horizontal">
<ListBox ItemsSource="{Binding Elements}"
SelectedItem="{Binding SelectedElement, Mode=TwoWay}" />
<ContentPresenter Content="{Binding SelectedElement}">
<ContentPresenter.Resources>
<DataTemplate DataType="{x:Type SandboxWpf:Element}">
<StackPanel Orientation="Horizontal">
<RadioButton Content="A" IsChecked="{Binding MemberA, Mode=TwoWay}"/>
<RadioButton Content="B" IsChecked="{Binding MemberB, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
</ContentPresenter.Resources>
</ContentPresenter>
</StackPanel>
</Window>
You can switch the A and B values using the radio buttons on each element. However, if you set the first element and second element to different values (one A and one B), then go back to the other selected element, the other element will now have both A and B set to false. Something wonky is happening when the SelectedElement property changes on the ContentPresenter that somehow triggers the TwoWay binding I think, causing it to overwrite the property values, but I can't figure out why this would happen or how to resolve it. This is a deliberately simplified version of something I'm seeing in a larger application.
Any idea what's happening here?
回答1:
Because they belong to same Group, see GroupName Property, you can set individual GroupName for each Element(every element has different group).
<DataTemplate DataType="{x:Type SandboxWpf:Element}">
<StackPanel Orientation="Horizontal">
<RadioButton GroupName="{Binding ElementGroupName}" Content="A" IsChecked="{Binding MemberA, Mode=TwoWay}"/>
<RadioButton GroupName="{Binding ElementGroupName}" Content="B" IsChecked="{Binding MemberB, Mode=TwoWay}"/>
</StackPanel>
</DataTemplate>
来源:https://stackoverflow.com/questions/19962966/radiobutton-twoway-binding-issues