Wpf 如何在ControlTemplate中绑定RadioButton
我正在使用带有controlTemplate的列表框来显示带有ListBoxItem的单选按钮。 在这里,我想将RadioButton的IsChecked属性设置为true,并为此进行必要的wpf绑定。下面是xamlWpf 如何在ControlTemplate中绑定RadioButton,wpf,data-binding,listbox,controltemplate,Wpf,Data Binding,Listbox,Controltemplate,我正在使用带有controlTemplate的列表框来显示带有ListBoxItem的单选按钮。 在这里,我想将RadioButton的IsChecked属性设置为true,并为此进行必要的wpf绑定。下面是xaml <ListBox ItemsSource="{Binding Products}" DisplayMemberPath="ProductName" Grid.Column="0" Width="250" HorizontalAlignment="Left" Margin="0
<ListBox ItemsSource="{Binding Products}" DisplayMemberPath="ProductName" Grid.Column="0" Width="250" HorizontalAlignment="Left" Margin="0,2,0,30" AlternationCount="2">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<RadioButton IsChecked="{Binding IsRadioButtonChecked, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
<ContentPresenter></ContentPresenter>
</RadioButton>
</ControlTemplate>
</Setter.Value>
</Setter>
<!--Alternate Style Indexing-->
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="LightGray"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
ListStyleViewModel类是
public class ListStylesViewModel : INotifyPropertyChanged
{
private string prodName;
private List<Products> products;
private bool isChecked = true;
public ListStylesViewModel()
{
ListStyleModel model = new ListStyleModel();
this.Products = model.GetProducts();
}
public string ProductName
{
get
{
return this.prodName;
}
set
{
this.prodName = value;
this.OnPropertyChanged("ProductName");
}
}
public List<Products> Products
{
get
{
return this.products;
}
set
{
this.products = value;
this.OnPropertyChanged("Products");
}
}
public bool IsRadioButtonChecked
{
get
{
return this.isChecked;
}
set
{
this.isChecked = value;
this.OnPropertyChanged("IsChecked");
}
}
#region
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string pptName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(pptName));
}
}
#endregion
公共类ListStylesViewModel:INotifyPropertyChanged
{
私有字符串;
私人上市产品;
private bool isChecked=真;
public ListStylesViewModel()
{
ListStyleModel模型=新的ListStyleModel();
this.Products=model.GetProducts();
}
公共字符串产品名称
{
得到
{
返回此文件。prodName;
}
设置
{
this.prodName=值;
此.OnPropertyChanged(“ProductName”);
}
}
公开上市产品
{
得到
{
退回本产品;
}
设置
{
这就是产品=价值;
本协议项下的不动产变更(“产品”);
}
}
已检查公共布尔值
{
得到
{
返回此。已检查;
}
设置
{
this.isChecked=值;
本条关于财产的变更(“已检查”);
}
}
#区域
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串pptName)
{
if(PropertyChanged!=null)
{
PropertyChanged(即新PropertyChangedEventArgs(pptName));
}
}
#端区
请告诉我这里做错了什么?绑定表达式
IsChecked="{Binding IsRadioButtonChecked,
RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
在ControlTemplate中,使用ListBoxItem作为源对象。但是,ListBoxItem没有IsRadioButtonChecked
属性
由于绑定源属性位于ListBox的DataContext
中的ListStylesViewModel实例中,因此绑定表达式应如下所示:
IsChecked="{Binding DataContext.IsRadioButtonChecked,
RelativeSource={RelativeSource AncestorType=ListBox}}"
我认为主要的问题是,您的
IsRadioButtonChecked
在ListStylesViewModel
中实现,而它应该在产品中实现,因此每个项目的选中状态都会被跟踪,而不是对整个列表框跟踪一次。我希望RadioButton只在ListStyleViewModel中实现,明白吗e立即回答。(需要在此控件上设置DataContext)请参阅
IsChecked="{Binding DataContext.IsRadioButtonChecked,
RelativeSource={RelativeSource AncestorType=ListBox}}"