【问题标题】:Binding UserControl properties within DataGridRow在 DataGridRow 中绑定 UserControl 属性
【发布时间】:2021-06-12 01:06:06
【问题描述】:

我需要在DataGrid 行中绑定UserControl 属性。我的 XAML 如下。 我有DirectionArrow 控件,它在每一行中显示不同的角度。如何将此绑定到角度属性 DataGrid ItemsSource 属性?

<DataGrid x:Name="DtgProfiles" Grid.Row="0"  HorizontalScrollBarVisibility="Auto" SelectionMode="Extended" ItemsSource="{Binding Project.SelectedAxis.Profiles}" CanUserDeleteRows="False" CanUserAddRows="False" CanUserSortColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="No" Width="4*" Binding="{Binding Path=ProfileOrder,Mode=TwoWay}"  IsReadOnly="True" />
        <DataGridTemplateColumn Header="Direction" Width="8*" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <controls:DirectionArrow
                        HorizontalAlignment="Center" Height="13" Margin="0,0,0,0"
                        VerticalAlignment="Center" Width="13" BackgroundFill="LightBlue"
                        LineAngle="{Binding Direction}" LineLength="1"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

【问题讨论】:

  • LineAngle="{Binding Direction}" 应该可以工作,前提是您没有犯常见的错误,即明确地将 UserControl 的 DataContext 分配给它自己或私有视图模型。
  • 您显示的代码不足以回答。添加描述细节:集合项Project.SelectedAxis.Profiles的实现(仅属性声明就足够了)和controls:DirectionArrow的实现(属性LineAngle和设置DataContext,如果有)
  • 我通过将属性直接添加到类而不是 vM 来解决这个问题

标签: c# wpf datagrid


【解决方案1】:

我通过将属性直接添加到类而不是 vM 来解决此问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 2011-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多