0

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"); }
    }
Andrea
  • 11,801
  • 17
  • 65
  • 72
denderp
  • 95
  • 8
  • You can do it! Read here: http://stackoverflow.com/questions/936304/binding-to-static-property – Tony Dec 20 '13 at 07:13
  • it execute the code only ones, it needs to update on a certain action – denderp Dec 20 '13 at 07:40
  • You need when 1 button is clicked, other buttons become disabled? – Tony Dec 20 '13 at 08:04
  • To get updates working you need to implement `INotifyPropertyChanged` which in short means not having a static property. Why does it need to be static? – Iain Dec 20 '13 at 08:04
  • other classes need to have access to it. mainwindow has the buttons and usercontrol has the datagrid. when clicked on row datagrid button becomes enabled and if == null then disable – denderp Dec 20 '13 at 08:09

4 Answers4

0

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}"/>
Tony
  • 7,345
  • 3
  • 26
  • 34
0

There is some issue on using Static Property in this case. 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 SelectedItemproperty as a normal Property which is used to bind to UIElement. I have also declared a Property called mySharedVariableToOtherClass which is Static and used to set the value inside from the SelectedItem property. This is Static, so you can access it from other classes too. In my openion Trigger is the right choice here to disable the Button control.

<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>
Vimal CK
  • 3,543
  • 1
  • 26
  • 47
0

Try the way mentioned in the following link

WPF TwoWay binding to a static property

Boopesh
  • 404
  • 2
  • 6
0

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"); 
    }
}
Wiley Marques
  • 485
  • 3
  • 16