【问题标题】:C# Resource Value Changed With ButtonC# 资源值随按钮更改
【发布时间】:2016-02-09 23:06:14
【问题描述】:

我有一个简单的 UWP 应用程序一个简单的计算器。所以我通过这样的样式来设置按钮的所有属性。

<Page.Resources>
    <Style TargetType="Button" x:Key="CalculatorButtons">
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="BorderBrush" Value="Black" />
        <Setter Property="Background" Value="AntiqueWhite" />
    </Style>
</Page.Resources>

我想制作一个按钮来更改我制作的样式中的背景属性的值。我的按钮代码是这样开始的

private void ColorChange_Click(object sender, RoutedEventArgs e)
{

}

我是新手,我找不到从这里访问和更改它的方法。

【问题讨论】:

    标签: c# button resources uwp


    【解决方案1】:

    你不能在运行时修改样式,唯一的办法就是创建新样式并替换旧样式。

    private void ColorChange_Click(object sender, RoutedEventArgs e)
    {
           var style = new Style(typeof(Button));
    
            style.Setters.Add(new Setter(FrameworkElement.VerticalAlignmentProperty, VerticalAlignment.Stretch));
            style.Setters.Add(new Setter(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
            style.Setters.Add(new Setter(Control.FontSizeProperty, 30.0));
            style.Setters.Add(new Setter(Control.BorderThicknessProperty, new Thickness(1.0)));
            style.Setters.Add(new Setter(Control.BorderBrushProperty, Brushes.Black));
    
            style.Setters.Add(new Setter(Control.BackgroundProperty, Brushes.Orange));
    
            this.Resources["CalculatorButtons"] = style;
    }
    

    请注意,在这种情况下您必须使用 DynamicResource,例如:

    <Button Style="{DynamicResource ResourceKey=CalculatorButtons}" ...
    

    【讨论】:

    • 好吧,我明白了,但在 UWP 中我不能使用 DynamicResource,这是我从 Visual Studio 得知的。该帖子已更改为 WPF sry,所以我唯一的选择是编写一个代码,该代码将分别更改每个按钮,我猜因为我无法使用此代码!
    【解决方案2】:

    好的,经过大量搜索,我在这里找到了我需要的一切http://blog.jerrynixon.com/2013/01/walkthrough-dynamically-skinning-your.html 这个想法非常简单。所以从我的风格中我删除了背景设置器并像这样制作了自己的资源。但是在它自己的字典中,我将其命名为 Style.Blue.xaml,因为它是蓝色的,我像 Jerry Nixon 所说的那样为我所有的颜色做了它。

    <SolidColorBrush x:Key="ButtonColor" Color="Blue" />
    

    我会在我的按钮中使用这一行访问它

    Background="{StaticResource ButtonColor}"
    

    之后我在我的按钮所在的 MainPage.xaml.cs 中创建了这个。

    void ChangeTheme(Uri source)
        {   // Recreated my Merged dictinaries at the app.xaml
    
            var _Custom = new ResourceDictionary { Source = source };
            var _Main = new ResourceDictionary { MergedDictionaries = { _Custom } };
            App.Current.Resources = _Main;
    
    
            // This is needed to basiclly Refresh the page since we use Static Resources.
            // so we navigate forth and back to the whole frame.
    
            var _Frame = Window.Current.Content as Frame;
            _Frame.Navigate(_Frame.Content.GetType());
            _Frame.GoBack();
        }
    

    然后我在每个按钮上添加了这段代码,并根据我想要的颜色更改了源代码(如果我想要蓝色,它是 -> Style.Blue.xaml for Red -> Style.Red.xaml)和 ms-appx :/StyleColors/ 是路径。我为我所有的样式创建了一个文件夹。

    private void ColorChange_Click(object sender, RoutedEventArgs e)
            {
                ChangeTheme(new Uri("ms-appx:/StyleColors/Style.Blue.xaml"));
            }
    

    现在只需一行代码,我就可以更改所有按钮的颜色或我需要的其他类型的数据。

    我希望它也能帮助更多的人。

    【讨论】:

      猜你喜欢
      • 2013-06-15
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 2012-02-05
      相关资源
      最近更新 更多