0

I created a ListView that shows some element names in a column and in a second column I want to select one item using a RadioButton. However the binding of the RadioButtons IsChecked property does not seem to work, the IsMaster property of the item in the collection is not changed.

I simplified my classes a bit. This is the struct used for the collection:

public struct SomeData
{
    public string Name
    {
        get;
        set;
    }

    public bool IsMaster
    {
        get;
        set;
    }
}

The ViewModel contains the collection:

public ObservableCollection<SomeData> DataCollection
{
    get;
    set;
}

The View contains the DateTemplates and die ListView:

<DataTemplate x:Key="NameTemplate">
    <TextBlock>
        <TextBlock.Text>
            <Binding Path="Name"/>
        </TextBlock.Text>
    </TextBlock>
</DataTemplate>

<DataTemplate x:Key="IsMasterTemplate">
    <RadioButton GroupName="MainGroup">
        <RadioButton.IsChecked>
            <Binding Path="IsMaster" Mode="TwoWay" NotifyOnTargetUpdated="True" />
        </RadioButton.IsChecked>
    </RadioButton>
</DataTemplate>

<ListView ItemsSource="{styleTemplates:WpfBinding Path=DataCollection}" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Name" Width="200" CellTemplate="{StaticResource NameTemplate}" />
            <GridViewColumn Header="Is Master" Width="140" CellTemplate="{StaticResource IsMasterTemplate}" />
        </GridView>
    </ListView.View>
</ListView>

What did I miss ?

Nico
  • 122
  • 1
  • 8
  • 1
    SomeData must implement the INotifyPropertyChanged interface and fire the PropertyChanged event in the setter of the IsMaster property. Besides that, setting `Mode="TwoWay"` and `NotifyOnTargetUpdated="True"` on the IsChecked Binding is pointless. The property binds TwoWay by default and setting NotifyOnTargetUpdated only makes sense when you also attach a TargetUpdated event handler. Just write ``. – Clemens May 04 '22 at 11:24
  • 1
    And declare SomeData as class, not as struct. Struct is a value type, not a reference type, and you would operate on different instances without noticing. – Clemens May 04 '22 at 11:30
  • Thanks for the advice, i changed the struct to class and implemented INotifyPropertyChanged and it works now. – Nico May 04 '22 at 12:19

0 Answers0