[英]binding a property of a button to static field class(MVVM)
i have buttons on my mainwindow and i want to enable and disable that control by a static field class. 我在主窗口上有按钮,我想通过静态字段类启用和禁用该控件。 is this possible without going to the code behind?
无需转到后面的代码,这是否可能?
mainwindow 主窗口
<Button x:Name="btnAanpassen" Content="Aanpassen" Grid.Row="1" Command="{Binding SaveItemCommand}" CommandParameter="{Binding SelectedItem}" IsEnabled="{Binding EnableDisable}"/>
my vm 我的虚拟机
private static object _selectedItem;
public static object SelectedItem
{
get { return _selectedItem; }
set {
if (SelectedItem != null)
{
//enable control
}
else
{
//disable control
}
_selectedItem = value; }
}
private Boolean _enableDisable;
public Boolean EnableDisable
{
get { return _enableDisable; }
set { _enableDisable = value; OnPropertyChanged("EnableDisable"); }
}
You can try this: 您可以尝试以下方法:
<Window.Resources>
<yourNS:YourMVVM x:Key="mvvm"/>
</Window.Resources>
... ...
<Button x:Name="btnAanpassen" Content="Aanpassen" Grid.Row="1" Command="{Binding SaveItemCommand}"
CommandParameter="{Binding Source={StaticResource mvvm}, Path=SelectedItem}" IsEnabled="{Binding EnableDisable}"/>
There is some issue on using Static Property
in this case. 在这种情况下,使用
Static Property
存在一些问题。 I have a workaround on this problem. 我有一个解决此问题的方法。 See below snippets suits for you.
请参阅以下适合您的摘录。
public partial class MainWindow : Window,INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static object mySharedVariableToOtherClass = value;
private object _selectedItem;
public object SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
mySharedVariableToOtherClass = null;
OnPropertyChanged("SelectedItem");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
SelectedItem = "vimal";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SelectedItem = null;
}
public void OnPropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(PropertyName));
}
}
}
In above snippet I have declared SelectedItem
property as a normal Property
which is used to bind to UIElement
. 在上面的代码段中,我已将
SelectedItem
属性声明为用于绑定到UIElement
的常规Property
。 I have also declared a Property
called mySharedVariableToOtherClass
which is Static
and used to set the value inside from the SelectedItem
property. 我还声明了一个名为
mySharedVariableToOtherClass
的Property
,该Property
是Static
,用于从SelectedItem
属性中设置值。 This is Static
, so you can access it from other classes too. 这是
Static
,因此您也可以从其他类访问它。 In my openion Trigger
is the right choice here to disable the Button
control. 在我的开篇中,
Trigger
是禁用Button
控件的正确选择。
<Grid Name="MainGrid">
<Button Width="100"
Height="40"
Click="Button_Click">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=SelectedItem}"
Value="{x:Null}">
<Setter Property="Button.IsEnabled"
Value="False" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<TextBlock Width="200"
Height="30"
Text="{Binding SelectedItem, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
Margin="74,46,69,154" />
</Grid>
Try the way mentioned in the following link 尝试以下链接中提到的方法
You can't bind directly to a static property. 您不能直接绑定到静态属性。 If you really need this property to be static, one approach is to create a proxy property that gets and sets it.
如果您确实需要将此属性设为静态,则一种方法是创建一个获取并设置它的代理属性。 It's based on the singleton pattern .
它基于单例模式 。
private static readonly ClassName _this = new ClassName();
public object ProxySelectedItem
{
get { return SelectedItem; }
set { SelectedItem = value; }
}
private static object _selectedItem;
public static object SelectedItem
{
get { return _selectedItem; }
set
{
if (SelectedItem != null)
{
_this.EnableDisable = true;
}
else
{
_this.EnableDisable = false;
}
_selectedItem = value;
_this.OnPropertyChanged("ProxySelectedItem");
}
}
private bool _enableDisable;
public bool EnableDisable
{
get { return _enableDisable; }
set
{
_enableDisable = value;
OnPropertyChanged("EnableDisable");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.