Bind Settings to a group of RadioButtons
I have a kind of annoying scenario here. In my WPF GUI I declared some RadioButtons, I want the right one to be checked when the GUI loads.
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" />
To keep possible bugs to a minimum I created a Property in my HonkySettings of the type String called TrayIconBehaviour. It contains the ContentProperty of the currently checked RadioButton.
I've been doing the loading programatically with a LoadSettings() function, but I'd like to remove that and do something more appealing with 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;
}
I've got HonkyGuiControls, which contains WPF User Controls that I use in my HonkySettingsWindow, if needed, I'd be ready to create a custom RadioButton to bind my settings to them.
I already tried to create a User Control called CustomRadioButton and bind its IsCheckedProperty to something like this:
public Boolean IsChecked
{
get
{
if (CustomRadioButton.Content.Equals(HonkySettings.Default.TrayIconBehaviour)) return true;
else return false;
}
set
{
HonkySettings.Default.TrayIconBehaviour = CustomRadioButton.Content.ToString();
}
}
But the CustomRadioButton IsChecked Property wouldn't bind to it, because I'd need to create a DependencyProperty, and I have no idea how I should do the same thing with a DependencyProperty. Help please.
If you want to this using binding, try this
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>
Converter
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();
}
}
if you making alot of this kind of binding as i can see there so to reduce xaml code we can do it this way as below
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();
}
}
converter
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/44668.html
上一篇: UWP XAML RadioButton IsChecked无法正常工作
下一篇: 将设置绑定到一组RadioButton