2017-06-12 8 views
0

ControlBoxでListBoxを使用して、ListBoxItemでRadioButtonを表示しています。 ここでは、RadioButtonのIsCheckedプロパティをtrueに設定し、これに必要なwpf Bindingを実行します。以下の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プロパティはありません。

バインディングソースプロパティは、リストボックスのDataContextにListStylesViewModelインスタンスであるので、あなたの結合式は次のようになります。

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

ありがとうございました。 – Arvind

+0

[私の質問に誰かが答えたときにどうすればいいですか?](https://stackoverflow.com/help/someone-answers)を参照してください。 – Clemens

関連する問題