我目前正在使用MVVM的WPF应用程序。我有与建立一个风格的列表框,使其显示像一个单选按钮列表如下:在风格中绑定命令
<Style x:Key="RadioButtonList" TargetType="{x:Type ListBox}">
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border Background="Transparent">
<RadioButton Focusable="False" IsHitTestVisible="False" IsChecked="{TemplateBinding IsSelected}" Content="{Binding Path=DisplayName}" Command="{Binding ElementName=ShippingWindow, Path=DataContext.ShipOtherMethodSelected}">
</RadioButton>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
<ListBox Name="lbShipOtherMethodOptions" Style="{StaticResource RadioButtonList}" ItemsSource="{Binding Path=ShipOtherMethodOptions}" Margin="13,74,366,282" />
我试图做的是绑定到一个单选按钮命令,让我可以关火了事件时进行选择。我有我的视图模型下面的代码,但我似乎无法得到它的火:
private ICommand shipOtherMethodSelected;
public ICommand ShipOtherMethodSelected
{
get
{
return shipOtherMethodSelected ??
(shipOtherMethodSelected = new RelayCommand(param => ShipOpenItems(), param => true));
}
}
private void ShipOpenItems()
{
MessageBox.Show("GOT HERE");
}
我很新的WPF和MVVM,所以我可能失去了一些东西明显。任何人都可以将我指向正确的方向吗?
编辑: 根据jberger的建议,我放入了一些我试过的代码,但没有奏效。在该部分中设置断点没有被触发,消息框也没有显示出来。
编辑2: 因此发件人检查的DataContext后,原来有人指着我正在绑定单选按钮的对象,而不是我的视图模型。我更新了上面的代码(将x:Name添加到我的窗口并更新了Command绑定),现在我得到的事件是在最初绑定时触发的,但是当我选择一个值时它不会触发。好像我们现在变得非常接近了。
你没有在你的xaml – 2012-02-28 20:57:52
中绑定'ShipOtherMethodSelected'抱歉,我试过了,但是我必须在复制/粘贴我的代码之前恢复。我会解决这个问题 – 2012-02-28 21:07:15
如果你删除'Focusable =“False”IsHitTestVisible =“False”',会发生什么?如果你钩入'Click'事件,点击时会触发吗? – 2012-02-28 21:15:53