c# - 如何编写数据触发器以通过在 WPF 中单击动态按钮打开弹出窗口?

标签 c# wpf dynamic popup datatrigger

我的 WPF 应用程序中有一个动态按钮,单击弹出窗口应打开的任何一个按钮时,相应的按钮应将背景颜色变为橙色,其余按钮应使用 DataTrigger 默认背景颜色还有一种情况,在Popup关闭时,相应的Button Background Color应该变成Default Color。

Note: I Can't able to give Name for the Buttons because of Dynamic Creation. Here I placed 5 Buttons without Name Property, Consider the Button as Dynamic Creation

我的 XAML 源代码是

<Grid>
    <Grid Height="30px" VerticalAlignment="Top" Margin="0,50,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100px"/>
            <ColumnDefinition Width="100px"/>
            <ColumnDefinition Width="100px"/>
            <ColumnDefinition Width="100px"/>
            <ColumnDefinition Width="100px"/>
        </Grid.ColumnDefinitions>
        <Button Content="One" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"/>
        <Button Content="Two" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"/>
        <Button Content="Three" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"/>
        <Button Content="Four" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"/>
        <Button Content="Five" Grid.Column="4" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75"/>
    </Grid>
    <Grid>
        <Popup Name="sPop" Placement="Mouse" StaysOpen="False">
            <Grid>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="Welcome to Popup Screen"/>
            </Grid>
        </Popup>
    </Grid>
</Grid>

最佳答案

您不能使用 DataTriggers 来做到这一点。 DataTriggers 适用于基于数据的场景,因此如果您使用 DataBinding,请使用它们。

推荐方法:

为您的弹出窗口指定一个名称。并使用行为。

<Button ...>
    <Button.Style>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:Interaction.Behaviors>
                <ei:ConditionBehavior>
                    <ei:ConditionalExpression>
                        <ei:ComparisonCondition LeftOperand="{Binding IsFocused, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" RightOperand="True"/>
                    </ei:ConditionalExpression>
                </ei:ConditionBehavior>
            </i:Interaction.Behaviors>
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=Popup1}" PropertyName="IsOpen" Value="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

使用 Blend 程序集:

添加对 System.Windows.InteractivityMicrosoft.Expression.Interactions 以及以下命名空间的引用:

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

还原源按钮的旧背景:

<Button>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <i:Interaction.Behaviors>
                <ei:ConditionBehavior>
                    <ei:ConditionalExpression>
                        <ei:ComparisonCondition LeftOperand="{Binding IsFocused, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" RightOperand="True"/>
                    </ei:ConditionalExpression>
                </ei:ConditionBehavior>
            </i:Interaction.Behaviors>

            <!-- Store Button's old Background -->
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=Popup1}" PropertyName="Tag" Value="{Binding Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"/>

            <!-- Change Button's Background -->
            <ei:ChangePropertyAction PropertyName="Background" Value="Purple"/>

            <!-- Open Popup -->
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=Popup1}" PropertyName="IsOpen" Value="True"/>

            <!-- Save this Button, Popup will use it to revert back its old Background -->
            <ei:ChangePropertyAction TargetObject="{Binding ElementName=Popup1}" PropertyName="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"/>

        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

<Popup x:Name="Popup1" Placement="Mouse" StaysOpen="False">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Closed">
            <ei:ChangePropertyAction 
                        TargetObject="{Binding PlacementTarget, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Popup}}" 
                        PropertyName="Background" 
                        Value="{Binding Tag, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Popup}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
    <Grid>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="Welcome to Popup Screen"/>
    </Grid>
</Popup>    

关于c# - 如何编写数据触发器以通过在 WPF 中单击动态按钮打开弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34304313/

相关文章:

c# - 替换是/否/取消 MessageBox (C#)

c# - 什么是 "index out of range"异常,我该如何解决?

c# - 从字符串创建类实例

c# - 从 Linq 查询调用方法

jquery - 使用 JQuery 新添加的具有多个 div 的元素

c# - 在 WPF 中动态添加和删除 xaml 结构

c# - 从单独的 MVVM 模块更新 MainWindow 属性

python - 如何从 IronPython 更新 WPF DataGrid 一行的值?

SQL查询问题

javascript - 动态定义 Mongoose 模型方法