将设置绑定到一组RadioButton
我在这里有一种令人讨厌的情景。 在我的WPF GUI中,我声明了一些RadioButton,我希望在加载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的字符串类型的我的HonkySettings中创建了一个属性。 它包含当前选中的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做同样的事情。 请帮助。
如果你想要使用绑定,请试试这个
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();
}
}
链接地址: http://www.djcxy.com/p/44667.html
上一篇: Bind Settings to a group of RadioButtons
下一篇: Radiobuttons Ischecked property is not working in wpf mvvm