トップ回答者
WPFでListView上に配置したTextBoxのForeground、Backgroundプロパティへのバインド方法について

質問
-
VS2008のWPFで開発を行なっていますが、
ListView上に配置したTextBoxの文字色と背景色を同一行で設定されるRGBの値により変更しようと思い、
下記のようにViewModelにSolidColorBrushのプロパティを定義し、Foreground、Backgroundプロパティにバインドしたのですが、画面を表示する時に
「'#00FFFFFF' 値をオブジェクト 'System.Windows.Controls.TextBox' のプロパティ 'Foreground' に割り当てることはできません。 このオブジェクトは別のスレッドに所有されているため、呼び出しスレッドはこのオブジェクトにアクセスできません。」
のエラーが発生し、画面が表示できません。
WPF開発歴3ヶ月の初心者なので、どこがおかしいのかわからず、困っています。
下記コードでおかしい箇所、またForeground、Backgroundプロパティへのバインド方法をご教示頂きたいのですが。
よろしくお願い致します。
「ViewModelコード」
private int _TextRed;
/// <summary>
/// text色(R)
/// </summary>
public int TextRed
{
get { return this._TextRed; }
set
{
if (this._TextRed != value)
{
this._TextRed = value;
this.SetTextColor();
base.FireNotifyPropertyChanged("TextRed");
}
}
}・・・TextRedと同様にTextGreen、TextBlueも定義
private SolidColorBrush _TextColor;
/// <summary>
/// 文字色
/// </summary>
public SolidColorBrush TextColor
{
get { return this._TextColor; }
set
{
this._TextColor = value;
base.FireNotifyPropertyChanged("TextColor");
}
}private void SetTextColor()
{
this.TextColor = new SolidColorBrush(Color.FromRgb(GetColorCode(this.TextRed, 0), GetColorCode(this.TextGreen, 0), GetColorCode(this.TextBlue, 0)));
}「Xamlコード」
TextBox
Text="{Binding Path=Comment}">
<TextBox.Foreground>
<SolidColorBrush Color="{Binding Path=TextColor}"/>
</TextBox.Foreground>
</TextBox>- 移動 山本春海 2012年5月1日 7:35 より適切と思われるカテゴリに移動しました。 (移動元:Silverlight - .NET)
回答
-
XAMLでSolidColorBrushを定義して、そのColorプロパティにバインドするなら、ViewModelのTextColorプロパティはSolidColorBursh型じゃなくてColor型でいいと思いますがどうでしょう。
かずき Blog:http://d.hatena.ne.jp/okazuki/ VS 2010のデザイナでBlendのBehaviorをサポートするツール公開してます。 http://vsbehaviorsupport.codeplex.com/
- 回答の候補に設定 山本春海 2012年5月22日 2:26
- 回答としてマーク ひらぽんModerator 2012年12月20日 2:20
-
Foregroundへのバインドの仕方ですが、直接ForegroundとModelにバインドしてみてはいかがでしょうか?
私の環境では以下でうまくいきます。(VS2010 Windows7)
Backgroundも同じです。
【View】
<Window x:Class="hogehoge.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">
<Grid>
<TextBox Name="tbx" Text="{Binding Path=Comment}" Foreground="{Binding Path=TextColor}"/>
</Grid>
</Window>【CodeBehind】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 hogehoge
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.tbx.DataContext = this;
}
private string _Comment = "コメント文字列";
public string Comment
{
get { return _Comment; }
set { _Comment = value; }
}
private SolidColorBrush _TextColor = new SolidColorBrush(Colors.Red);
public SolidColorBrush TextColor
{
get { return _TextColor; }
set { _TextColor = value; }
}
}
}
- 回答としてマーク ひらぽんModerator 2012年12月20日 2:20
すべての返信
-
XAMLでSolidColorBrushを定義して、そのColorプロパティにバインドするなら、ViewModelのTextColorプロパティはSolidColorBursh型じゃなくてColor型でいいと思いますがどうでしょう。
かずき Blog:http://d.hatena.ne.jp/okazuki/ VS 2010のデザイナでBlendのBehaviorをサポートするツール公開してます。 http://vsbehaviorsupport.codeplex.com/
- 回答の候補に設定 山本春海 2012年5月22日 2:26
- 回答としてマーク ひらぽんModerator 2012年12月20日 2:20
-
Foregroundへのバインドの仕方ですが、直接ForegroundとModelにバインドしてみてはいかがでしょうか?
私の環境では以下でうまくいきます。(VS2010 Windows7)
Backgroundも同じです。
【View】
<Window x:Class="hogehoge.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">
<Grid>
<TextBox Name="tbx" Text="{Binding Path=Comment}" Foreground="{Binding Path=TextColor}"/>
</Grid>
</Window>【CodeBehind】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 hogehoge
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.tbx.DataContext = this;
}
private string _Comment = "コメント文字列";
public string Comment
{
get { return _Comment; }
set { _Comment = value; }
}
private SolidColorBrush _TextColor = new SolidColorBrush(Colors.Red);
public SolidColorBrush TextColor
{
get { return _TextColor; }
set { _TextColor = value; }
}
}
}
- 回答としてマーク ひらぽんModerator 2012年12月20日 2:20