将设置绑定到一组RadioButtons

时间:2014-08-10 18:52:56

标签: c# wpf xaml

我这里有一种讨厌的场景。 在我的WPF GUI中,我声明了一些RadioButtons,我希望在GUI加载时检查正确的。

XAML:

<RadioButton Grid.Row="0" Grid.Column="0" Name="RadioButtonShowSettings" GroupName="OnTrayClick" Content="Show settings window" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="1" Grid.Column="0" Name="RadioButtonOpenFile" GroupName="OnTrayClick" Content="Open upload dialog" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="2" Grid.Column="0" Name="RadioButtonIndexFile" GroupName="OnTrayClick" Content="Open file indexer" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="0" Grid.Column="1" Name="RadioButtonImageGallery" GroupName="OnTrayClick" Content="Open image gallery" HorizontalAlignment="Left" VerticalAlignment="Center" />
<RadioButton Grid.Row="1" Grid.Column="1" Name="RadioButtonTakeScreenshot" GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)" HorizontalAlignment="Left" VerticalAlignment="Center" />

为了将可能的错误保持在最低限度,我在名为TrayIconBehaviour的String类型的HonkySettings中创建了一个Property。它包含当前检查的RadioButton的ContentProperty。

我一直在使用LoadSettings()函数以编程方式进行加载,但是我想删除它并使用Bindings做一些更有吸引力的事情。

LoadSettings:

private void LoadSettings()
{
    List<RadioButton> TrayIconBehaviourRadioButtons = GridTrayIconBehaviour.Children.OfType<RadioButton>().ToList();
    foreach (RadioButton rButton in TrayIconBehaviourRadioButtons)
    {
        if (rButton.Content.Equals(HonkySettings.Default.TrayIconBehaviour))
            rButton.IsChecked = true;
    }

    List<RadioButton> FullscreenCaptureRadioButtons = GridFullscreenCapture.Children.OfType<RadioButton>().ToList();
    foreach (RadioButton rButton in FullscreenCaptureRadioButtons)
    {
        if (rButton.Content.Equals(HonkySettings.Default.FullscreenCapture))
            rButton.IsChecked = true;
    }

    if (RadioButtonQualityPNG.Content.Equals(HonkySettings.Default.ScreenCaptureQuality))
        RadioButtonQualityPNG.IsChecked = true;
    else RadioButtonQualityJPG.IsChecked = true;
}

我有HonkyGuiControls,其中包含我在HonkySettingsWindow中使用的WPF用户控件,如果需要,我已准备好创建一个自定义RadioButton来将我的设置绑定到它们。

我已经尝试创建一个名为CustomRadioButton的用户控件,并将其IsCheckedProperty绑定到这样的内容:

public Boolean IsChecked
{
    get
    {
        if (CustomRadioButton.Content.Equals(HonkySettings.Default.TrayIconBehaviour)) return true;
        else return false;
    }
    set
    {
        HonkySettings.Default.TrayIconBehaviour = CustomRadioButton.Content.ToString();
    }
}

但是CustomRadioButton IsChecked属性不会绑定它,因为我需要创建一个DependencyProperty,而我不知道我应该如何使用DependencyProperty做同样的事情。请帮助。

1 个答案:

答案 0 :(得分:2)

如果你想使用绑定,请尝试这个

  

XAML

<Window x:Class="Stackoverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Stackoverflow"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:SettingsConverter x:Key="settingsConverter"/>
</Window.Resources>
<StackPanel>
    <RadioButton   GroupName="OnTrayClick" Content="Show settings window">
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource myNameConverter}">
                <Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
                <Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </RadioButton.IsChecked>
    </RadioButton>
    <RadioButton   GroupName="OnTrayClick" Content="Open upload dialog">
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource myNameConverter}">
                <Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
                <Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </RadioButton.IsChecked>
    </RadioButton>
    <RadioButton   GroupName="OnTrayClick" Content="Open file indexer">
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource myNameConverter}">
                <Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
                <Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </RadioButton.IsChecked>
    </RadioButton>
    <RadioButton   GroupName="OnTrayClick" Content="Open image gallery">
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource myNameConverter}">
                <Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
                <Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </RadioButton.IsChecked>
    </RadioButton>
    <RadioButton   GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)">
        <RadioButton.IsChecked>
            <MultiBinding Converter="{StaticResource myNameConverter}">
                <Binding Path="HonkySettings.Default.TrayIconBehaviour"/>
                <Binding Path="Content" RelativeSource="{RelativeSource Mode=Self}"/>
            </MultiBinding>
        </RadioButton.IsChecked>
    </RadioButton>
</StackPanel>

  

转换器

    public class SettingsConverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null && values.Count() == 2)
            return values.First() == values.Last();
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}    

如果你做了很多这种绑定,因为我可以看到那里为了减少xaml代码我们可以这样做,如下所示

  

XAML

<Window x:Class="Stackoverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Stackoverflow"
    Title="MainWindow" Height="350" Width="525">
<StackPanel>
    <RadioButton   GroupName="OnTrayClick" Content="Show settings window" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
    <RadioButton   GroupName="OnTrayClick" Content="Open upload dialog" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
    <RadioButton   GroupName="OnTrayClick" Content="Open file indexer"  IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
    <RadioButton   GroupName="OnTrayClick" Content="Open image gallery"  IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
    <RadioButton   GroupName="OnTrayClick" Content="Take a screenshot (3 seconds delay)" IsChecked="{local:RadioButtonBinding HonkySettings.Default.TrayIconBehaviour}"/>
</StackPanel>

  

RadioButtonBinding

    public class RadioButtonBinding : MultiBinding
{
    public RadioButtonBinding(string propName)
    { 
        Bindings.Add(new Binding(propName));
        Bindings.Add(new Binding("Content") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });
        Converter = new SettingsConverter();
    }
}
  

转换器

    public class SettingsConverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null && values.Count() == 2)
            return values.First() == values.Last();
        return false;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}