将 Boder.Background 从资源字典绑定到 LinearGradientBrush

发布于 2024-12-12 00:17:44 字数 1067 浏览 0 评论 0原文

我想将边框属性的背景绑定到列表中的元素。

我有一个包含以下内容的字典:

<LinearGradientBrush x:Key="ConfigurationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFAABBCC" Offset="1"/>
    <GradientStop Color="#FFCCDDEE" Offset="0.7"/>
</LinearGradientBrush>    

<LinearGradientBrush x:Key="NavigationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFD97825" Offset="1"/>
    <GradientStop Color="#FFFF9A2E" Offset="0.7"/>
</LinearGradientBrush>

现在我填充一个 ObservableCollection,其中包含包含名为“BackgroundStyle”的属性的对象。当我用样式背景填充列表框时,我想将背景绑定到“BackgroundStyle”,

<Border x:Name="Border" BorderThickness="1" CornerRadius="4" Width="120" Height="80"
        VerticalAlignment="Center" HorizontalAlignment="Center" Padding="4"
        BorderBrush="Black" Background="{Binding Path=BackgroundStyle}">

如果BackgroundStyle =“Red”或“Green”,则效果很好,但如果我使用“ConfigurationItemBackground”,则它将不起作用。

有什么建议吗? 感谢您的帮助;)

-蒂姆-

I'd like to Bind the Background of a Border Property to elements in a list.

I have a Dictionary holding the follwing:

<LinearGradientBrush x:Key="ConfigurationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFAABBCC" Offset="1"/>
    <GradientStop Color="#FFCCDDEE" Offset="0.7"/>
</LinearGradientBrush>    

<LinearGradientBrush x:Key="NavigationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#FFD97825" Offset="1"/>
    <GradientStop Color="#FFFF9A2E" Offset="0.7"/>
</LinearGradientBrush>

Now I fill a ObservableCollection holding Objects that contain a Property called "BackgroundStyle". When I fill a list box with styled background I'd like to bind the Background to "BackgroundStyle"

<Border x:Name="Border" BorderThickness="1" CornerRadius="4" Width="120" Height="80"
        VerticalAlignment="Center" HorizontalAlignment="Center" Padding="4"
        BorderBrush="Black" Background="{Binding Path=BackgroundStyle}">

This works well, if BackgroundStyle="Red" or "Green" but it won't work if I use "ConfigurationItemBackground".

Any suggestions?
Thanks for your help ;)

-Tim-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-12-19 00:17:44

您无法完全执行您想要执行的操作,这本质上是使用数据绑定将资源的存储在绑定对象中。您能得到的最接近的答案就是这个问题,它基本上使用 ValueConverter 从应用程序的资源中获取资源。

不幸的是,这不适用于您想要做的事情,即使用本地资源。为此,您最好编写自己的自定义 ValueConverter 来将您的 string 值转换为画笔。

可以做一些像这样的通用操作:

[ValueConversion(typeof(string), typeof(object))]
public class ResourceKeyConverter : IValueConverter
{
    public ResourceKeyConverter()
    {
        Resources = new ResourceDictionary();
    }

    public ResourceDictionary Resources { get; private set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    { 
        return Resources[(string)value];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

然后像这样使用它:(

<Window.Resources>
    <my:ResourceKeyConverter x:Key="keyConverter">
        <ResourceKeyConverter.Resources>
            <LinearGradientBrush x:Key="ConfigurationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFAABBCC" Offset="1"/>
                <GradientStop Color="#FFCCDDEE" Offset="0.7"/>
            </LinearGradientBrush>    

            <LinearGradientBrush x:Key="NavigationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFD97825" Offset="1"/>
                <GradientStop Color="#FFFF9A2E" Offset="0.7"/>
            </LinearGradientBrush>
        </ResourceKeyConverter.Resources>
    </my:ResourceKeyConverter>
</Window.Resources>

...

<Border Background="{Binding BackgroundStyle, Converter={StaticResource keyConverter}}">
</Border>

我在家,面前没有 Visual Studio,所以这可能需要一些调整,但应该是基本上是正确的)。

You can't do exactly what you're trying to do, which is essentially to use data binding to store the key of a resource within your bound object. The closest you can get would be the answer to this question, which basically uses a ValueConverter to get the resource from the application's resources.

Unfortunately, this won't work with what you want to do, which is use a local resource. For that, you'd be better off writing your own custom ValueConverter that converts your string value into a brush.

You could do something generic like this:

[ValueConversion(typeof(string), typeof(object))]
public class ResourceKeyConverter : IValueConverter
{
    public ResourceKeyConverter()
    {
        Resources = new ResourceDictionary();
    }

    public ResourceDictionary Resources { get; private set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    { 
        return Resources[(string)value];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException();
    }
}

Then use it like this:

<Window.Resources>
    <my:ResourceKeyConverter x:Key="keyConverter">
        <ResourceKeyConverter.Resources>
            <LinearGradientBrush x:Key="ConfigurationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFAABBCC" Offset="1"/>
                <GradientStop Color="#FFCCDDEE" Offset="0.7"/>
            </LinearGradientBrush>    

            <LinearGradientBrush x:Key="NavigationItemBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFD97825" Offset="1"/>
                <GradientStop Color="#FFFF9A2E" Offset="0.7"/>
            </LinearGradientBrush>
        </ResourceKeyConverter.Resources>
    </my:ResourceKeyConverter>
</Window.Resources>

...

<Border Background="{Binding BackgroundStyle, Converter={StaticResource keyConverter}}">
</Border>

(I'm at home and don't have Visual Studio in front of me, so this might require some tweaking, but it should be largely correct).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文