代码之家  ›  专栏  ›  技术社区  ›  Rodney

将一种画笔绑定到背景(或在代码中设置绑定)

  •  1
  • Rodney  · 技术社区  · 14 年前

    我想让用户自定义我的应用程序的背景,并选择一种颜色或一个不透明度绑定的图像。如果可能的话,我想用XAML做这个。

    我想写一个转换器来返回合适的画笔,但是我不知道如何将不透明度应用于背景(它适用于所有内容)。

    <navigation:Frame>
        <navigation:Frame.Background>
            <SolidColorBrush Color="{Binding Config.BackgroundColorInfo.Value}" Opacity="{Binding Config.BackgroundOpacity}" />
            <!--<ImageBrush ImageSource="/TourneyManager;component/image.JPG" Stretch="UniformToFill" Opacity="{Binding Config.BackgroundOpacity}"/>-->
        </navigation:Frame.Background>
        <navigation:Frame.UriMapper>
    

      SolidColorBrush backgroundBrush = new SolidColorBrush();
    Binding b = new Binding("Config.BackgroundColorInfo.Value");
    ContentFrame.SetBinding(SolidColorBrush.ColorProperty, b);
    
    Binding b1 = new Binding(".BackgroundOpacity");
    ContentFrame.SetBinding(SolidColorBrush.OpacityProperty, b1);
    
    ContentFrame.Background = backgroundBrush;
    

    但是SolidColorBrush类没有SetBinding方法,因此没有乐趣。

    1 回复  |  直到 14 年前
        1
  •  3
  •   AnthonyWJones    14 年前

    为了在一个 DependencyObject 这不是源于 FrameworkElement 你需要使用 BindingOperations.SetBinding

    SolidColorBrush backgroundBrush = new SolidColorBrush();
    Binding b = new Binding("Config.BackgroundColorInfo.Value");
    BindingOperations.SetBinding(backgroundBrush, SolidColorBrush.ColorProperty, b);
    

    注意:文档指出,如果目标不是类型,则会出现异常 框架元件 但是文档与Silverlight4的功能不同步。

    编辑

    既然你已经有了一个你作为 Config Brush ?