急需帮助绑定颜色. [英] Need help binding a color urgent.

查看:89
本文介绍了急需帮助绑定颜色.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have searched high and low and cannot seem to figure this out. Can someone please assist me? Here is what I am trying to bind. I have created sliders in settings.xaml.cs and added those to a solid brush color. I cannot access them from anywhere except code behind. Is there a way to call them from xaml?

    public void sli_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
    {
        SolidColorBrush FontBrush = (SolidColorBrush)this.Resources["FontBrush"];
        //SolidColorBrush FontBrush = (SolidColorBrush)TryFindResource("FontBrush");

        if ((sliR != null) && (sliG != null) && (sliB != null))
        {
            FontBrush.Color = Color.FromRgb((byte)sliR.Value, (byte)sliG.Value, (byte)sliB.Value);
        }

        settings.FontBrush = FontBrush;
    }
}

这是创建新笔刷的地方.可以在后面的代码中调用它.但是除了settings.xaml之外,没有其他XAML.

This is where the new brush is created. and it can be called upon in code behind. But not in any XAML except settings.xaml.

我正赶在完成这项工作的截止日期之前.任何帮助将不胜感激.

I am on a deadline to get this done...any help would be greatly appreciated.

推荐答案

尝试在资源字典中声明它

Try declaring it in a resource dictionary. 

最简单的方法是在表达式混合中做到这一点.您先选择控件,然后选择属性,然后单击属性窗口中画笔"旁边的小圆圈,然后选择转换为新资源".从这很明显

The easiest way is to do it in expression blend.  You select the control, then the property and then you can click on the little circle next to the Brush in the properties window, and select "Convert To New Resource" and it is obvious from there. 

然后,作为资源,您可以在xaml中轻松使用它.

Then, as a resource, you can easily use it in xaml.

以下是它为您带来的收益的一个例子.

Here is an example of what it will produce for you.

< SolidColorBrush x:Key ="Brush4"
                          Color =黑色"; />

 <SolidColorBrush x:Key="Brush4"
                         Color="Black" />

它必须在"Control.Resource"内部定义.或Window.Resource(如果您在同一xaml页面上本地定义).例如

It has to be defined inside of either a "Control.Resource" or a Window.Resource, if you define it on the same xaml page, locally. such as 


< Window.Resources>

<Window.Resources>

< SolidColorBrush x:Key ="MyCustomBrush"
                          Color =蓝色"; />

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

</Window.Resources>

</Window.Resources>

如果您将背后的代码绑定到xaml,就好像它是一个视图模型一样,在后面的代码中定义它们后,也可以从xaml调用它们.

It is also possible to call them from xaml, after defining them in the code behind, if you bind your codebehind to the xaml, as if it were a viewmodel.


这篇关于急需帮助绑定颜色.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆