【问题标题】:SolidColorBrush color change at runtimeSolidColorBrush 在运行时改变颜色
【发布时间】:2011-06-07 14:22:36
【问题描述】:

我在我的应用资源文件中有刷:

<SolidColorBrush x:Key="MainColor" Color="#FF15428B" /> 

我想在运行时改变这个画笔的颜色。我添加了颜色选择器 - 当用户选择颜色时,我希望这个画笔选择颜色。

我试过这样的代码:

SolidColorBrush MainColor = new SolidColorBrush(SelectedColor);

但它没有用。

【问题讨论】:

    标签: wpf xaml c#-4.0


    【解决方案1】:

    您需要设置现有画笔的Color属性。

    你可以写(SolidColorBrush)Resources["MainColor"]获取实例

    【讨论】:

    • 非常感谢!有效 :)。我使用的代码:Application.Current.Resources["MainColor"] = new SolidColorBrush(SelectedColor);
    【解决方案2】:

    您可以使用 TryFindResource 方法从代码隐藏中访问资源:

    SolidColorBrush myBrush = (SolidColorBrush)this.TryFindResource("myBrush");
    
    if (myBrush != null)
    {
        myBrush.Color = Colors.Yellow  ;
    }
    

    【讨论】:

      【解决方案3】:
      MainColor = new SolidColorBrush(Color.FromArgb(
          SelectedColor.A, SelectedColor.R, SelectedColor.G, SelectedColor.B
      ));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-22
        • 2010-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多