简体   繁体   中英

WPF Datatrigger on button based on datagrid

Currently, I have the following datatrigger:

<Style TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=ScheduleDataGrid, Path=HasItems}"
                        Value="false">
            <Setter Property="Button.IsEnabled" Value="false"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

and I have two buttons that adds/deletes a row in a datagrid

<Button Name="BtnAddPoint" Content="Add" Width="70" Margin="10 0 10 0" Click="BtnAddSchedule_Click"></Button>
<Button Name="BtnDeletePoint" Content="Delete" Width="70" Click="BtnDeleteSchedule_Click"></Button>

I have two questions.

Currently, the above trigger disables both of the button when I only want it to disable the delete button. Setting the targetname of the setter to the delete button doesn't work. Can I make the trigger target a particular button?

Also, I'd like the delete button to be only enabled when a grid item is selected rather than checking for the item count. Is this possible?

You should use Style with key if you want to assign it to appropriate button:

<Style x:Key="DeleteButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Button.IsEnabled" Value="True" />
    <Style.Triggers>              
        <DataTrigger Binding="{Binding ElementName=ScheduleDataGrid, Path=SelectedItem}" Value="{x:Null}">
            <Setter Property="Button.IsEnabled" Value="False" />
        </DataTrigger>            
    </Style.Triggers>
</Style>

In BtnDeletePoint button you should add style:

<Button Name="BtnAddPoint" Content="Add" Width="70" Margin="10 0 10 0" Click="BtnAddSchedule_Click"></Button>
<Button Name="BtnDeletePoint" Content="Delete" Width="70" Click="BtnDeleteSchedule_Click" Style="{StaticResource DeleteButtonStyle}"></Button>

Instead of checking HasItems and writing another trigger to check if grid has selected item, you can write trigger and check if SelectedItem is null. SelectedItem property gives you information if grid has items and if user selected one of them.

If both Button s share the same Style and therefore have the same Trigger , then they are both going to be disabled under the same circumstances (in this case, when the DataGrid has 0 items).

In order to disable the Delete Button under different circumstances you will need to create a separate Style with a different Trigger and apply that style to BtnDeletePoint. Since I don't see you setting the Style in the declaration of the buttons, I would guess the trigger belongs to an Implicit Style for Button , so you'll need to assign an x:Key to the new style so that you can assign it to your delete button:

<Style x:Key="DeleteButtonStyle" TargetType="Button">
    <!-- Setters -->
    <!-- Triggers -->
</Style>

<Button Name="BtnDeletePoint" Style="{DynamicResource DeleteButtonStyle}" Content="Delete" Width="70" Click="BtnDeleteSchedule_Click"/>

As for the trigger to enable the delete button when only a single item is selected, if you don't want to use the Count of the SelectedItems property on the DataGrid , then you would need to use a Converter to determine the selection state of the grid - there are no other DependencyProperties on the DataGrid that I'm aware of that will provide you that information.

Also, I'm not sure of the context without more of the code, but don't believe you want a DataTrigger in this case - you should be fine with a standard Trigger .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM