【问题标题】:How can I bind a RadioButton in a ControlTemplate如何在 ControlTemplate 中绑定 RadioButton
【发布时间】:2017-06-12 06:35:23
【问题描述】:

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

<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

请指教我在这里做错了什么?

【问题讨论】:

  • 我认为主要问题是,您的IsRadioButtonChecked 是在ListStylesViewModel 内部实现的,而它应该在Products 内部实现,因此每个项目的检查状态都会被跟踪,而不是一次整个列表框。
  • 我希望 RadioButton 仅在 ListStyleViewModel 中实现,现在得到答案。(需要在此控件上设置 DataContext)

标签: wpf data-binding listbox controltemplate


【解决方案1】:

绑定表达式

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

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

由于绑定源属性位于 ListBox 的 DataContext 中的 ListStylesViewModel 实例中,因此您的绑定表达式应如下所示:

IsChecked="{Binding DataContext.IsRadioButtonChecked,
                    RelativeSource={RelativeSource AncestorType=ListBox}}"
猜你喜欢
  • 1970-01-01
  • 2013-02-23
  • 2018-11-08
  • 2015-11-17
  • 2017-10-29
  • 2017-04-05
  • 1970-01-01
  • 2014-09-13
  • 1970-01-01
相关资源
最近更新 更多