本文介绍了在影响另一个对象的一个​​对象上创建一个简单的 wpf 触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我最接近创建一个简单触发器的方法.我只希望数据网格的 IsMouseOver == true 显示按钮.问题在于 Setter 的 TargetName 说:属性TargetName"不代表Setter"的有效目标,因为找不到名为ButtonExpand"的元素.确保在任何使用它的 Setter、触发器或条件之前声明目标.我做错了什么?

This is the closest that I have come to creating a simple trigger on this. I just want the datagrid's IsMouseOver == true to show the button.The problem is that the Setter's TargetName says: The property 'TargetName' does not represent a valid target for the 'Setter' because an element named 'ButtonExpand' was not found. Make sure that the target is declared before any Setters, Triggers or Conditions that use it.What am I doing wrong?

<UserControl.Resources>
    <Style TargetType="DataGrid">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter TargetName="ButtonExpand" Property="Visibility" Value="Visible" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid>



    <DataGrid Name="MainDataGrid" ItemsSource="{Binding Programs}" IsReadOnly="True" AutoGenerateColumns="false" >
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
            <DataGridTextColumn Header="Version" Binding="{Binding Version}"/>
            <DataGridTextColumn Header="Publisher" Binding="{Binding Publisher}"/>
        </DataGrid.Columns>
    </DataGrid>

    <Button Name="ButtonExpand" Height="25" Width="25" HorizontalAlignment="Right" VerticalAlignment="Bottom" Visibility="Hidden">+</Button>
</Grid>

推荐答案

请参阅链接.

您可以通过为 Button 设置 DataTrigger 来反其道而行之.请注意,您必须在 Style 中设置 Property Visibility 才能使 DataTrigger 工作.

You can do it the other way around with a DataTrigger for the Button. Note that you must set the Property Visibility within the Style for the DataTrigger to work.

<Grid Name="MainGrid">

    <DataGrid ItemsSource="{Binding Programs}"
              IsReadOnly="True"
              AutoGenerateColumns="false" >
      <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Version" Binding="{Binding Version}"/>
        <DataGridTextColumn Header="Publisher" Binding="{Binding Publisher}"/>
      </DataGrid.Columns>
    </DataGrid>

    <Button Name="ButtonExpand"
            Height="25"
            Width="25"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Content="+">
        <Button.Style>
            <Style TargetType="Button">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=MainGrid,
                                                   Path=IsMouseOver}"
                                 Value="True">
                        <Setter Property="Visibility" Value="Visible" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>
</Grid>

另一种方法是使用转换器将 ButtonExpand 的 Visibilty 绑定到 DataGrid 的 IsMouseOver 属性.

Another way to do it would be to bind Visibilty of ButtonExpand to the IsMouseOver property of the DataGrid with a converter.

这篇关于在影响另一个对象的一个​​对象上创建一个简单的 wpf 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 00:24