user1175338
user1175338

Reputation: 79

Radiobutton databinding don't work in wpf

I have such "VM" and "Model" code, thats represent Product class with implementation of INotifyPropertyChanged:

Model:

public class Product : INotifyPropertyChanged
{
    public Product(string name, bool isEnable)
    {
        Name = name;
        IsEnable = isEnable;
    }
    public string Name { get; set; }
    private bool _isEnable;
    public bool IsEnable
    {
        get { return _isEnable; }
        set
        {
            if (value != _isEnable)
            {
                _isEnable = value;
                OnPropertyChanged("IsEnable");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

ViewModel:

    public ObservableCollection<Product> Products
    {
        get { return _products; }
        set { _products = value;}
    }
    private ObservableCollection<Product> _products = new ObservableCollection<Product>()
        {
            new Product("a", false),
            new Product("b", false),
            new Product("c", false),
        };

And View with ListBox where each Item is RadioButton:

<Window.Resources>
     <Style x:Key="RadioButtonListStyle" TargetType="{x:Type ListBox}">
        <Setter Property="SelectionMode" Value="Single"></Setter>
        <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}">
                                <RadioButton GroupName="Gr1" 
                                      IsChecked="{Binding Path=IsEnable,RelativeSource={RelativeSource TemplatedParent},
                                      UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
                                 <ContentPresenter></ContentPresenter>
                               </RadioButton>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Margin="5">
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <ListBox x:Name="UserList" Style="{StaticResource RadioButtonListStyle}" ItemsSource="{Binding Products}">
    </ListBox>
</Grid>

But nothing happened every time when I click to the radiobutton. I could not reach to a breakpoint in the setter method on debug mode.

Upvotes: 0

Views: 1198

Answers (1)

PGallagher
PGallagher

Reputation: 3113

You should be referring your IsEnable Binding directly to the bound object (Product), rather than to the Templated Parent;

<RadioButton GroupName="Gr1" IsChecked="{Binding Path=IsEnable}, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">

Upvotes: 1

Related Questions