繁体   English   中英

WPF Datagrid - 订阅单元格加载事件?

[英]WPF Datagrid - subscribe to cell load event?

我希望在单元格加载后修改代码隐藏中的数据网格单元格的布局。

我知道我可以订阅DataGrid.LoadDataGrid.LoadingRow事件,但我希望有一种方法可以订阅特定的单个单元格的加载事件,以便实现更清晰。 文档中 ,看起来单元类具有事件(继承自FrameworkElement)但我找不到干净地订阅事件的方法。

我正在使用DataGridTemplateColumn,我认为在XAML(CellTemplate)中这样做很容易。

<DataGridTemplateColumn Header="ATTENDEES" Width="*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=attendees}" x:Name="AttendeesItemsControl"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

谢谢!

您可以将DataGridColumn.CellStyleEventSetter一起使用来实现:

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="Loaded" Handler="CellLoaded" />
        </Style>
    </DataGridTemplateColumn.CellStyle>
</DataGridTemplateColumn>

在代码隐藏中:

private void CellLoaded(object sender, EventArgs e)
{
    var cell = (DataGridCell)sender;
    ...
}

暂无
暂无

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

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