WPF单选按钮 - MVVM - 结合似乎死吗? [英] WPF radio buttons - MVVM - binding seems to die?
问题描述
我必将以下窗口到code的的DataContext
背后给我一个MVVM 风格
来说明这种情况:
I've bound the DataContext
of the following Window to the code behind to give me a MVVM style
to demonstrate this behaviour:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<RadioButton GroupName="test" Content="Monkey" IsChecked="{Binding IsMonkey}"/>
<RadioButton GroupName="test" Content="Turtle" IsChecked="{Binding IsTurtle}" />
</StackPanel>
</Window>
继承人背后的code:
Heres the code behind:
public partial class Window1
{
public Window1()
{
InitializeComponent();
}
private bool _isMonkey;
public bool IsMonkey
{
get { return _isMonkey; }
set
{
_isMonkey = value;
}
}
private bool _isTurtle;
public bool IsTurtle
{
get { return _isTurtle; }
set
{
_isTurtle = value;
}
}
}
把一个断点设置 IsMonkey
和 IsTurtle
,然后运行该应用程序并选择 IsMonkey
和 IsTurtle
后,对方我发现,它为第一选择每个控制的,第二选择绑定的休息时间和断点不再触发?
Putting a breakpoint on the set of IsMonkey
and IsTurtle
and then running the application and selecting IsMonkey
and IsTurtle
after each other I found that it works for the first selection of each control, on the second selection the binding breaks and the breakpoints are no longer triggered?
任何人都可以点我朝着正确的方向吗?
Can anyone point me in the right direction, please?
推荐答案
您例如没有更改的通知。你写的只是一个MVVM风格建筑的例子。因此,我认为,你已经实现了 INotifyPropertyChanged的
或者财产DependencyProperties。如果没有,你必须做的第一件事就是更改通知。
Your example has no Changed-notification. Your wrote it's only an example for an MVVM-style construction. Therefore I assume, you have implemented INotifyPropertyChanged
or the properties are DependencyProperties. If not, the first thing you have to do is change-notification.
如果您有更改通知,给不同的单选按钮组名称(另一名称为每个实例)。这样可以使它们和结合将不再断开。
If you have change-notification, give the RadioButtons different group names (another name for each instance). This decouples them and the binding will not be broken anymore.
<StackPanel>
<RadioButton GroupName="test1" Content="Monkey" IsChecked="{Binding IsMonkey}"/>
<RadioButton GroupName="test2" Content="Turtle" IsChecked="{Binding IsTurtle}" />
</StackPanel>
根据你的属性的声明,也可能是meaningfull申报双向绑定。
Depending on the declaration of your properties, it may also be meaningfull to declare the Binding TwoWay.
IsChecked="{Binding IsMonkey,Mode=TwoWay}
希望这有助于。
Hope this helped.
这篇关于WPF单选按钮 - MVVM - 结合似乎死吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!