Reputation: 443
I would like to bind the colour of the border to the value of a radio button in the border. If the button is true - selected- I would like to change the default colour of the border from blue to red.
<Border x:Name="RibbonMenuRight"
Background="Blue"
Margin="5">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<RadioButton Margin="0,0,0,10"
VerticalAlignment="Center"
HorizontalAlignment="Center"
GroupName="directions"
DataContext="{Binding UserSiteSettings}"
IsChecked="{Binding ExpanderLocation, ConverterParameter=Right,Converter=StaticResource ExpanderValueToBoolConverter}, Mode=TwoWay}" />
<TextBlock Text="Right " HorizontalAlignment="Center"/>
</StackPanel>
</Border>
Any help would be appreciated
regards mike
Upvotes: 2
Views: 127
Reputation: 102753
It looks like you can use an ElementName binding with a converter:
<Border x:Name="RibbonMenuRight"
Background="{Binding ElementName=ColorRadioButton,Path=IsChecked,Converter={StaticResource BoolToColorConverter}}" ...>
<Border.Resources>
<ResourceDictionary>
<local:BoolToColorConverter x:Key="BoolToColorConverter" />
</ResourceDictionary>
</Border.Resources>
</Border>
<RadioButton x:Name="ColorRadioButton" ... />
The converter should return the corresponding brush:
public class BoolToColorConverter : IValueConverter
{
private static Brush _trueBrush = new SolidColorBrush(Colors.Red),
_falseBrush = new SolidColorBrush(Colors.Blue);
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool isChecked = (value as bool?) ?? false;
return (isChecked ? _trueBrush : _falseBrush);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Upvotes: 1