WPF Comparison Binding

There is a relatively simple thing I'm trying to achieve, but I'm not sure how to do it. Basically, I have a CLR class as follows:

class SomeClass
{
    public SomeEnum Status;
}

public enum SomeEnum { One, Two, Three };

I have a DataGrid that I bind ObservableCollection<SomeClass>programmatically through code. In this DataGrid I have DataGridTemplateColumn, containing two buttons:

<toolkit:DataGridTemplateColumn Header="Actions">
    <toolkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
        <Button Content="ActionOne" />
        <Button Content="ActionTwo" />
    </StackPanel>
    </DataTemplate>
</toolkit:DataGridTemplateColumn.CellTemplate>
</toolkit:DataGridTemplateColumn>

What I want to do is bind the IsEnabled property of these buttons to a comparison based on the value of {Binding Path = Status}. For example, in pseudocode:

ActionOne.IsEnabled = BoundValue.Status != SomeEnum.Two
ActionTwo.IsEnabled = BoundValue.Status == SomeEnum.One || BoundValue.Status == SomeEnum.Two

Is there anyway to do this in XAML? An alternative would be to simply write a value converter for each button, but since the contents and other details of the button can change, I also do not want to write as 6 value converters.

Hooray!

+3
2

SomeClass, ?

:

public bool ActionOneEnabled
{
    get { return Status != SomeEnum.Two; }
}

Button IsEnabled .

OnPropertyChanged ( "ActionOneEnabled" ), .

+4

DataTrigger , . Bryan , , , .

<Button>
    ....
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="IsEnabled" Value="False" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Status, Converter={StaticResource yourConverter}}" Value="True">
                    <Setter Property="IsEnabled" Value="True" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>

- DataTrigger IsEnabled:

<Button  
    IsEnabled="{Binding Path=Status, Converter={StaticResource yourConverter}}"  
    ...  
/> 
+4

Source: https://habr.com/ru/post/1727563/


All Articles