简体   繁体   English

WPF Datatrigger on button基于datagrid

[英]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. 将setter的targetname设置为delete按钮不起作用。 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分配给适当的按钮,则应使用Style with key:

<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: BtnDeletePoint按钮中,您应该添加样式:

<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是否为空,而不是检查HasItems并编写另一个触发器来检查网格是否已选择项目。 SelectedItem property gives you information if grid has items and if user selected one of them. 如果网格包含项目,并且用户选择了其中一项,则SelectedItem属性会为您提供信息。

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). 如果两个Button共享相同的Style并因此具有相同的Trigger ,那么它们将在相同的情况下被禁用(在这种情况下,当DataGrid有0个项目时)。

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. 为了在不同情况下禁用“删除” Button ,您需要使用不同的Trigger创建单独的Style ,并将该样式应用于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 ,我猜测触发器属于Button的隐式样式,所以你需要为新样式分配一个x:Key ,这样你就可以分配它到你的删除按钮:

<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. 至于在仅选择单个项目时启用删除按钮的触发器,如果​​您不想在DataGrid上使用SelectedItems属性的Count ,则需要使用Converter来确定选择状态。 grid - 我知道DataGrid上没有其他DependencyProperties可以为你提供这些信息。

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 . 另外,如果没有更多的代码,我不确定上下文,但是在这种情况下你不相信你想要一个DataTrigger - 你应该没有标准的Trigger

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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