Silverlight边框背景颜色绑定

时间:2015-01-09 14:11:37

标签: silverlight binding

我想将边框的颜色绑定到边框中单选按钮的值。如果按钮为真 - 选中 - 我想将边框的默认颜色从蓝色更改为红色。

<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>

任何帮助将不胜感激

问候 麦克

1 个答案:

答案 0 :(得分:1)

看起来您可以将ElementName绑定与转换器结合使用:

<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" ... />

转换器应返回相应的画笔:

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();
    }
}