Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#与静态资源绑定_C#_Wpf_Mvvm - Fatal编程技术网

C#与静态资源绑定

C#与静态资源绑定,c#,wpf,mvvm,C#,Wpf,Mvvm,我需要设置控件的背景色,这取决于标尺的力度。所以,我尝试使用转换器来实现这一点 在我的XAML中: <TextBox Background="{Binding Converter={StaticResource BackgroundConverter}, ConverterParameter='UserName'}"> 它在第一次显示屏幕时工作,但我需要在用户编辑表单、取消等时更新这些属性 如何设置绑定以实现此目的?您可以在viewmodel中创建一个新属性并将其称为State,然

我需要设置控件的背景色,这取决于标尺的力度。所以,我尝试使用转换器来实现这一点

在我的XAML中:

<TextBox Background="{Binding Converter={StaticResource BackgroundConverter}, ConverterParameter='UserName'}">
它在第一次显示屏幕时工作,但我需要在用户编辑表单、取消等时更新这些属性


如何设置绑定以实现此目的?

您可以在viewmodel中创建一个新属性并将其称为State,然后返回person.State。当您更改person对象的状态时,只需调用PropertyChanged(“状态”)

在您的视图中绑定文本框

<TextBox Background="{Binding State,Converter={StaticResource BackgroundConverter}, ConverterParameter='UserName'}">


这将使代码正常工作。

绑定到视图模型中的属性,并根据需要进行更新。在这种情况下,您没有转换器。请注意,属性必须通知更改(请参阅InotifyPropertyChanged)。@paulo它不知道
Person.state
已更改,因为它绑定到
PersonBase
。在绑定值更改之前,它不会触发。我可以绑定到一个值,但可以访问转换器中的Person类吗?您可以使用
public class YourViewModel : INotifyPropertyChanged
{
   // ... your code here
   public StateObjectType State
   {
       get {return person.state;}
   }

   // when you modify the person state just call OnPropertyChanged("State")
}
<TextBox Background="{Binding State,Converter={StaticResource BackgroundConverter}, ConverterParameter='UserName'}">