Alex
Alex

Reputation: 505

WPF RadioButton - Override state change

In .NET Framework v4.0, is it possible to override the state change of a WPF RadioButton?

In the XAML below, I'm using a Listbox to display a dynamic number of Items, of which a single item is deemed the "Selected Item".

<ListBox Height="Auto"
         Name="listBoxItems"
         ItemsSource="{Binding Mode=OneWay, Path=Items}"
         SelectedItem="{Binding Path=UserSelectedItem}">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal">
        <RadioButton GroupName="SameGroup" Checked="OnItemSelected" IsChecked="{Binding Mode=TwoWay, Path=IsSelected}" CommandParameter="{Binding}"/>
        <TextBlock Text="{Binding Mode=OneTime, Converter={StaticResource itemDescriptionConverter}}"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

When clicking a RadioButton, the OnItemSelected method will do some validation, and then provide a Dialog box informing the User that the new "Selected Item" will be saved.

In the event of an Error condition, or the User cancelling the Save, I was wanting the RadioButton state change to be reset / overridden. ie I manually change the value of the IsSelected property.

Debugging this through I see the following sequence of Events.

  1. Radio button is checked causing IsSelected property change value, and a NotifyPropertyEvent to be fired
  2. The new value of the IsSelected property is read.
  3. The OnSelected method is called, resulting in a Dialog box.
  4. The user cancels the action, and I manually call IsSelected on each bound object, resetting values back. This fires off multiple NotifyPropertyEvents.
  5. The reset values are NEVER re-read.

Upvotes: 1

Views: 2181

Answers (1)

paparazzo
paparazzo

Reputation: 45106

I have some code where I clear any RadioButtons and it is working for me. Review your code. The event is NotifyPropertyChanged not NotifyProperty.

<ListBox ItemsSource="{Binding Path=cbs}" SelectionMode="Single">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <RadioButton GroupName="UserType" Content="{Binding Path=name}" IsChecked="{Binding Path=chcked, Mode=TwoWay}" Checked="RadioButton_Checked" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


    public class cb: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        private bool c = false;
        public bool chcked 
        {
            get { return c; }
            set 
            {
                if (c == value) return;
                c = value;
                NotifyPropertyChanged("chcked");
            } 
        }
        public string name { get; private set; }
        public cb(string _name) { name = _name; }
    }

    private void btnClickClearAll(object sender, RoutedEventArgs e)
    {
        foreach (cb c in cbs.Where(x => x.chcked))
        {
            c.chcked = false;
        }
    }

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        if (cbs[0].chcked) cbs[0].chcked = false;   
    }

Upvotes: 2

Related Questions