将 Boder.Background 从资源字典绑定到 LinearGradientBrush
我想将边框属性的背景绑定到列表中的元素。
我有一个包含以下内容的字典:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法完全执行您想要执行的操作,这本质上是使用数据绑定将资源的键存储在绑定对象中。您能得到的最接近的答案就是这个问题,它基本上使用
ValueConverter
从应用程序的资源中获取资源。不幸的是,这不适用于您想要做的事情,即使用本地资源。为此,您最好编写自己的自定义
ValueConverter
来将您的string
值转换为画笔。您可以做一些像这样的通用操作:
然后像这样使用它:(
我在家,面前没有 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 yourstring
value into a brush.You could do something generic like this:
Then use it like this:
(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).