2016-06-20 22 views
-1

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; } 
      } 
     } 
    } 
} 
+0

こちらをご覧ください:http://stackoverflow.com/q/13262037/1136211 – Clemens

+0

私は前にこの記事を見てきましたが、それはおよそdynamicalyリソースを作成しています。それは別のアプローチです。 – thowa

答えて

0

新しいプロパティを作成BackgroundBrushと呼ばれ、ブラシにあなたの文字列をキャストするために、このコードを使用します。

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

バインディングキーワードのみを使用してバインドする(不要)StaticResourceへ:

{Binding BackgroundBrush} 
1

ちょうどあなたのBackgroundBrushにプロパティをバインドします。

<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> 

そしてBrushにあなたのStringプロパティを変更します。

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; } 
       } 
      } 
     } 
+0

私は実際にどこかにあらかじめ定義されたブラシを持っていて、各行に新しいものを作成したくないと思っています。私はパフォーマンスを低下させたくないからです。 – thowa

+0

@thowa行ごとに新しいSolidColorBrushを作成する必要はありません。代わりにBrushインスタンスを再利用してください。 2つの線が同じ色を持つ必要がある場合は、同じSolidColorBrushインスタンスをBackgroundプロパティに割り当てます。それ以外にも、それらをリソースとして定義することができ、 '(Brush)Resources [" BlueBrush "]によってコードの後ろに置くことができます。ビューモデルプロパティでブラシインスタンスを参照することは、そのリソースキーの文字列を保持する以上のコストはかかりません。 – Clemens

関連する問題