1
votes

I have two radio buttons in a group as part of my XAML project:

<StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
    <RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
    <RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
</StackPanel>

And I then have a command later on which refers to these IsChecked bindings:

public void CopyToClipboard(object o)
{
    if (TextViewIsSelected == true)
    {
        Clipboard.SetText(myFile.TextContent);
    }
    else if (XmlViewIsSelected == true)
    {
        Clipboard.SetText(myFile.XMLContent);
    }
}

However, the XmlViewIsSelected is permanently True and TextViewIsSelected is always false, no matter which radio button is selected. What am I missing?

1
Did you set the correct DataContext? Additionally, I am not sure about the default UpdateSourceTrigger for Radio buttons but you can try specifying it to "PropertyChanged" explicitley. Please post your DataContext (ViewModel) Code too - the two properties "TextviewIsSelected" and "XmlViewIsSelected"Luchspeter

1 Answers

2
votes

I think you are mispelling XmlViewIsChecked with XmlViewIsSelected The following for me is working

    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center" Orientation="Horizontal" Grid.Column="0" Margin="20,0,0,0">
        <RadioButton x:Name="XMLViewButton" GroupName="DisplayType" IsChecked="{Binding XmlViewIsChecked, FallbackValue=True, Mode=TwoWay}" Content="XML View" Margin="0,0,5,0"/>
        <RadioButton x:Name="TextViewButton" GroupName="DisplayType" IsChecked="{Binding TextViewIsChecked, FallbackValue=False, Mode=TwoWay}" Content="Text View" Margin="5,0,0,0"/>
        <Button Content="Check" Click="Button_Click" />
    </StackPanel>

public partial class MainWindow : Window
{
    public bool XmlViewIsChecked { get; set; }
    public bool TextViewIsChecked { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        if (TextViewIsChecked)
        {
            Clipboard.SetText("text");
        }
        else if (XmlViewIsChecked)
        {
            Clipboard.SetText("xml");
        }
    }
}