1
votes

I am having quite a bit of trouble with a radio button group. initially the radio buttons do not set the value from the ViewModel but when selected function properly. so basically the user does not know what is the initial value.

here is my xaml.

<ListBox ItemsSource="{Binding ViewModelCollection}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <RadioButton GroupName="rbList" IsChecked="{Binding Path=IsReady,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                            <Label Content="{Binding Path=NameOfRadioButton}"></Label>
                        </RadioButton>
                    </DataTemplate>
                </ListBox.ItemTemplate>    
            </ListBox>

here is my ViewModel

public bool IsReady
        {
            get
            {
                return BusinessObject.IsReady;  // debuggin on this line shows a true value being returned
            }
            set
            {
                BusinessObject.IsReady = value;
                OnPropertyChanged("IsReady");
            }
        }

the Viewmodel is a simple bool value what implements INotifyPropertyChanged. i doubt i can use an enum converter as the amount of radio buttons is always dynamic.

So just to clarify, on first run no radio button is selected but after click the radiobuttons work fine. and the viewmodel and underlying database reflect the changes made.

How can i get the radio buttons to show value on startup?

cheers.

3
You'd need to show additional code of when the view model gets created. At a guess from what you've said, I'd say the bool isn't being set a value at startup, so just defaults to False.Harry
sorry it gets it from the Business Object and debugging shows a true value coming in. I'll update the code to show this.BastanteCaro
ok. as of now i have changed the radiobuttons to check boxes. this doesn't really give me the functionality want but at least it shows the correctly selected items when the data loads.BastanteCaro

3 Answers

2
votes

Had the same problem with using the WPF toolkit themes (http://wpf.codeplex.com/). I compared the control template of the CheckBox (which worked) and the RadioButton (which did not work). The trigger for the IsChecked value is set to work opposite of the checkbox, it looks for "false" and then performs the checked animation on exit, where checkbox looks for true.

This causes a problem when a radio button is bound before it is rendered since the value would already be true the animation to set the checked visual state wouldn't fire until you checked another box to make it false.

I fixed the radio button theme by making it follow the same visual state logic as the checkbox. I am not sure if this breaks any other features of the radio button, but it seems to be working fine so far.

1
votes

I agree with the answer provided by user2174874. All I wish to do is add the xaml code that set things straight.

Problematic Code:

    <Trigger Property="IsChecked" Value="false">

        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.EnterActions>

    </Trigger>
    <Trigger Property="IsChecked" Value="True" />

Corrected Code:

    <Trigger Property="IsChecked" Value="True">
        <Trigger.ExitActions>
            <BeginStoryboard x:Name="CheckedOff_BeginStoryboard" Storyboard="{StaticResource CheckedOff}" />
        </Trigger.ExitActions>
        <Trigger.EnterActions>
            <BeginStoryboard x:Name="CheckedOn_BeginStoryboard" Storyboard="{StaticResource CheckedOn}" />
        </Trigger.EnterActions>
    </Trigger>
0
votes

I have created ViewModelCollection as ObservableCollection with test objects in ViewModel's ctor and it worked for me so i think you should fetch data from database there and RaisePropertyChanged of ViewModelCollection.