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



我在将颜色从后面的颜色绑定到定义为XAML中的颜色的颜色时遇到麻烦。绑定适用于文本(又称消息),但我无法为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; }
            }
        }
    }
}

只需将您的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; }
                }
            }
        }

创建一个名为 BackgroundBrush的新属性,并使用此代码将字符串施放到刷子:

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

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

{Binding BackgroundBrush}

最新更新