2017-06-12 81 views
0

我正在使用带有controlTemplate的ListBox来显示带有ListBoxItem的RadioButton。 这里我想将RadioButton的IsChecked属性设置为true,并为此执行必要的wpf绑定。下面是XAML隐藏类此如何绑定ControlTemplate中的RadioButton

<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> 

代码是

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = new ListStylesViewModel(); 
    } 
} 

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 

请指教一下我做错了吗?

+0

我认为主要的问题是,你的'IsRadioButtonChecked'的内部实现'ListStylesViewModel'应该在'Products'内部实现,所以在整个列表框中为每个项目跟踪检查状态,而不是一次。 – grek40

+0

我希望RadioButton仅在ListStyleViewModel中实现,现在得到了答案(需要在此控件上设置DataContext) – Arvind

回答

0

在ControlTemplate中的结合表达

IsChecked="{Binding IsRadioButtonChecked, 
        RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" 

使用ListBoxItem中作为源对象。但是,ListBoxItem没有IsRadioButtonChecked属性。

由于绑定源属性是在ListBox的DataContext的ListStylesViewModel例如,你的绑定表达式应该是这样的:

IsChecked="{Binding DataContext.IsRadioButtonChecked, 
        RelativeSource={RelativeSource AncestorType=ListBox}}" 
+0

谢谢......它的工作方式如此。 – Arvind

+0

请参阅[当某人回答我的问题时该怎么办?](https://stackoverflow.com/help/someone-answers) – Clemens

相关问题