如何从后面的代码绑定到XAML资源颜色

时间:2016-06-20 08:44:58

标签: c# wpf xaml binding

我无法将代码后面的颜色绑定到XAML中定义为资源的颜色。 绑定适用于文本(也称为Message),但我无法完成XAML中定义的颜色。 这是我正在使用的精简代码。

XAML

<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
 <Window.Resources>
        <SolidColorBrush x:Key="BlueBrush" Color="#FFCFEDFF" />
        <SolidColorBrush x:Key="GreenBrush" Color="#FFE5EFC8" />
 </Window.Resources>

 <Grid>
    <ListBox ItemsSource="{Binding List, ElementName=UI}" x:Name="listBox" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.Background>
>>>                         <SolidColorBrush Color="{StaticResource {Binding Path=Background}}"/>    <<< Here is my problem <<<
                        </Grid.Background>
                     <TextBlock Text="{Binding Message}"/>
                 </Grid>
             </DataTemplate>
         </ListBox.ItemTemplate>
     </ListBox>
 </Grid>
</Window>

背后的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        private ObservableCollection<Line> buffer;   

        public MainWindow()
        {
            InitializeComponent();
            buffer = new ObservableCollection<Line>();

            listBox.ItemsSource = buffer;
            buffer.Add(new Line("Line1", "BlueBrush"));
            buffer.Add(new Line("Line2", "GreenBrush"));
        }

        public class Line
        {
            private string _message;
            private string _background;

            public Line(String message, String background)
            {
                this._message = message;
                this._background = background;
            }

            public string Message
            {
                get { return _message; }
                set { _message = value; }
            }

            public string Background
            {
                get { return _background; }
                set { _background = value; }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

只需将Background绑定到Brush媒体资源。

<Grid>
    <ListBox  ItemsSource="{Binding List, ElementName=UI}" x:Name="listBox" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="{Binding Background}">
                    <TextBlock Text="{Binding Message}"/>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

并将您的String媒体资源更改为Brush

 public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                buffer = new ObservableCollection<Line>();

                listBox.ItemsSource = buffer;
                buffer.Add(new Line("Line1", new SolidColorBrush(Colors.Blue)));
                buffer.Add(new Line("Line2", new SolidColorBrush(Colors.Green)));
            }
            private ObservableCollection<Line> buffer;   

            public class Line
            {
                private string _message;
                private Brush _background;

                public Line(String message, Brush background)
                {
                    this._message = message;
                    this._background = background;
                }

                public string Message
                {
                    get { return _message; }
                    set { _message = value; }
                }

                public Brush Background
                {
                    get { return _background; }
                    set { _background = value; }
                }
            }
        }

答案 1 :(得分:0)

创建一个名为BackgroundBrush的新属性,并使用此代码将字符串转换为画笔:

public Brush BackgroundBrush => return (SolidColorBrush)new BrushConverter().ConvertFromString(this.Background);

仅使用绑定关键字绑定到它(不需要StaticResource):

{Binding BackgroundBrush}