【问题标题】:adding eventsetter for datarow not firing为未触发的数据行添加事件设置器
【发布时间】:2017-05-18 06:02:49
【问题描述】:

在 app.xaml 内部我有 datagrid 样式 对于每个包含数据网格的窗口,我为数据网格添加了一个事件设置器。 但是,当我运行它时,我可以看到样式,但是当我尝试触发事件时没有任何反应。如果我从 app.xaml 中删除样式。该活动运作良好。 为什么 app.xaml 样式禁用我的事件设置器? 这是 app.xaml 中的数据网格:

<Style TargetType="DataGrid">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="GridLinesVisibility" Value="None"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="RowStyle">
            <Setter.Value>
                <Style TargetType="DataGridRow">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="Foreground" Value="White"/>                        
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="ColumnHeaderStyle">
            <Setter.Value>
                <Style TargetType="DataGridColumnHeader">
                    <Setter Property="Margin" Value="5,0,0,0"/>
                    <Setter Property="Foreground" Value="White"/>
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{ x:Type DataGridColumnHeader}">
                                    <Border BorderThickness="2,2,2,0" CornerRadius="5,5,0,0">
                                    <Border.Style>
                                        <Style TargetType="Border">
                                            <Setter Property="BorderBrush" Value="White"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}" Value="{x:Null}">
                                                    <Setter Property="BorderBrush" Value="Transparent"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </Border.Style>
                                    <ContentPresenter HorizontalAlignment="Center" Margin="5"></ContentPresenter>
                                    </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
    </Style>

这是每个包含数据网格的窗口内的事件设置器:

<Style TargetType="DataGridRow">
        <EventSetter Event="MouseDoubleClick" Handler="EditItem"/>
    </Style>

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    看起来,网格使用其属性中的 RowStyle 比它在窗口资源中设置的时间晚。如果您清除网格中的 RowStyle 属性,它将起作用。把它放到窗口的资源中:

    <Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}">
        <Setter Property="RowStyle" Value="{x:Null}"/>
    </Style>
    

    为了可以使用 app.xaml 中设置的样式,您必须将 RowStyle 从属性移动到全局(在 app.xaml 中):

        <Style TargetType="DataGrid">
    ....
                    <Setter Property="RowStyle" Value="{x:Null}"/>
    ...
                </Style>
    
    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Foreground" Value="White"/>
    </Style>
    

    在窗口的资源中:

    <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
                            <EventSetter Event="MouseDoubleClick" Handler="PreviewMouseDown"/>
                        </Style>
    

    【讨论】:

    • 它不起作用我在窗口的资源中设置它,事件有效但行样式没有根据 app.xaml 改变
    • @user7925257 我已经修改了代码,因为您可以使用 app.xaml 中的样式作为行。
    • 所以正如你所说,我需要在每个数据网格中设置行样式?而不是一个地方?
    • 你可以把它放在窗口/父元素的资源中
    • 我添加的代码将无法正常工作,因为您将 RowStyle 设置为 DataGrid 的样式,而我基于全局 RowStyle.. 我已经更新了我的答案..
    猜你喜欢
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 2011-08-23
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多