【问题标题】:How to bind to XAML resource color from code behind如何从后面的代码绑定到 XAML 资源颜色
【发布时间】:2016-06-20 08:44:58
【问题描述】:

我无法将代码后面的颜色绑定到 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; }
            }
        }
    }
}

【问题讨论】:

标签: c# wpf xaml binding


【解决方案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; }
                }
            }
        }

【讨论】:

  • 我实际上想在某处预定义刷过的,而不是为每一行创建新的。因为我不想降低性能。
  • @thowa 您不需要为每条线创建一个新的 SolidColorBrush。而是重用 Brush 实例。当两条 Lines 应该具有相同的颜色时,将相同的 SolidColorBrush 实例分配给它们的 Background 属性。除此之外,您仍然可以将它们定义为资源并在(Brush)Resources["BlueBrush"] 后面的代码中访问它们。通过视图模型属性引用 Brush 实例的成本并不比为其资源键保存一个字符串多。
【解决方案2】:

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

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

只使用绑定关键字绑定到它(不需要静态资源):

{Binding BackgroundBrush}

【讨论】:

    猜你喜欢
    • 2017-07-24
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    • 2011-08-15
    • 2020-02-19
    • 2012-11-26
    相关资源
    最近更新 更多