2013-12-20 98 views
0

我在我的主窗口上有按鈕,我想通過靜態字段類來啓用和禁用該控件。這是可能的,而不需要背後的代碼?將按鈕屬性綁定到靜態字段類(MVVM)

主窗口

<Button x:Name="btnAanpassen" Content="Aanpassen" Grid.Row="1" Command="{Binding SaveItemCommand}" CommandParameter="{Binding SelectedItem}" IsEnabled="{Binding EnableDisable}"/> 

我的虛擬機

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

你可以做到!閱讀這裏:http://stackoverflow.com/questions/936304/binding-to-static-property – Tony

+0

它執行代碼只有一個,它需要更新一定的行動 – denderp

+0

你需要當1按鈕被點擊,其他按鈕變成禁用? – Tony

回答

0

你可以試試這個:

<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}"/> 
+0

仍然不更新 – denderp

0

在這種情況下使用Static Property有一些問題。我有一個解決這個問題的方法。請參閱下面的摘錄適合你。

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));  
     } 
    } 
} 

在上面的代碼中我已經聲明SelectedItem屬性作爲被用於結合UIElement正常Property。我還宣佈了Property,稱爲mySharedVariableToOtherClass,它是Static,用於從SelectedItem屬性中設置值。這是Static,所以你也可以從其他類訪問它。在我的公開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> 
+0

更新答案! –

0

提到的方式。 如果你真的需要這個屬性是靜態的,一種方法是創建一個代理屬性來獲取和設置它。它基於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"); 
    } 
}