【问题标题】:Custom WPF usercontrol within a datagrid cell - How does this child control get the row object or Id on an event?数据网格单元格中的自定义 WPF 用户控件 - 此子控件如何获取事件的行对象或 Id?
【发布时间】:2011-10-20 17:39:09
【问题描述】:

所以我有一个自定义控件:

<Grid>
    <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
        <CheckBox x:Name="chkboxListen" HorizontalAlignment="Center" Checked="chkboxListen_Checked" Unchecked="chkboxListen_Unchecked"/>
        <MediaElement x:Name="mediaElementAudioPlayer" Volume="{Binding ElementName=sliderVol, Path=Value}" />
        </StackPanel>
</Grid>

它将驻留在 DataGrid 模板列中:

            <DataGridTemplateColumn x:Name="callListenL"  Header="Listen(L)" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <localControls:AudioPlay x:Name="audioPlayL" localControls:AudioPlay>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

我的问题是:

当我检查 chkboxListen CheckBox 时,如何从 DataGrid (Parent) 的行中获取信息?每行都有一个带有 Id 的 myObject。我只需要那个 ID。

提前谢谢你。

【问题讨论】:

    标签: wpf datagrid checkbox


    【解决方案1】:

    您可以在用户控件中公开一个属性,并在数据网格数据绑定时设置该属性的值。

    例如,假设您的用户控件的属性名为 ParentRowId,您可以在后面的代码中设置它或使用 Eval 表达式

     <DataGridTemplateColumn x:Name="callListenL"  Header="Listen(L)" IsReadOnly="False">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <localControls:AudioPlay x:Name="audioPlayL" ParentRowId='<%# Cint(Eval("Id")) %>' localControls:AudioPlay>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
    

    【讨论】:

    • 谢谢,成功了。 ParentRowId="{绑定路径=Id}"
    猜你喜欢
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多