使用我的 UserControl 的内容加载 DataGrid 的行

Load a DataGrid's row with my UserControl's content

我有这个DataGrid:

<DataGrid x:Name="ButtonsDataGrid" Grid.Row="1" Margin="5,5,0,0"
          ScrollViewer.VerticalScrollBarVisibility="Auto"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled"
          Background="Transparent" HeadersVisibility="None"
          BorderThickness="0" BorderBrush="Transparent">
    <DataGridTemplateColumn Header="user_buttons">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <userButtons:GuestButtons/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid>

以及我需要放入其中的 XAML UserControl DataTemplate:

<UserControl x:Class="HP_hf_shop.BrowseButtons.GuestButtons"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:HP_hf_shop.BrowseButtons"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Button Template="{DynamicResource TileTemplate}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="5*"/>
            </Grid.ColumnDefinitions>
            <Path Data="M1.5,38.001 L10.666,38.001 10.666,38.005341 C10.666,38.011758 10.666,38.015 10.666,38.015 L57.666903,38.001107 67.909877,38.001 67.909877,81.667664 1.5,81.667664 z M34.188718,1.5000001 C46.680964,1.4988451 57.432162,7.8204405 57.710986,14.7386 57.917159,19.854203 57.674712,37.439694 57.667087,37.987992 L57.666903,38.001 10.666,38.001 10.666,37.976643 C10.665999,37.189652 10.665995,24.468745 10.666321,16.211438 10.666653,7.8482163 20.950168,1.501225 34.188718,1.5000001 z" 
                Style="{DynamicResource MimicVectorStyle}" Stretch="Fill"  StrokeThickness="3" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin"
                   Margin="5,2,5,2" Grid.Column="0"/>
            <Label Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"
                   HorizontalContentAlignment="Left" VerticalContentAlignment="Center"
                   Content="REGISTER" FontSize="30" FontFamily="CenturyGothicRegual"
                   Margin="0,0,0,0" Style="{DynamicResource MimicLabelStyle}">
            </Label>
        </Grid>
    </Button>
</Grid>

问题是 DataGrid 行中没有任何内容,即使我的 UserControl 中的 button 在设计器中可见。稍后这将必须是动态的,这就是我不只是在我提到的行内键入按钮的原因,因为我会有一个供客人、管理员和客户使用的按钮列表。那么,如何让 XAML UserControl 的内容出现在我的 DataGrid 的一行中?

我加了

HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"

DataGrid 中,但问题仍然存在。

代码编译 运行s 没有任何错误。

我创建了一个全新的解决方案并复制粘贴了 this MSDN article 中的 XAML 代码,没有任何改变。即使我 运行 这个项目,它也没有给出他们的结果。我在 Visual Studio 2015,我要重新安装吗? :D

我复制了样本并修复了两个问题:

1) DataGrid 没有设置 ItemsSource。如何修复:

xaml:

<DataGrid x:Name="ButtonsDataGrid" 
          ItemsSource="{Binding Path=???-some-collection-from-DataContext}" ...

或代码:

ButtonsDataGrid.ItemsSource = someCollection;

2) 设置了 ItemsSource 我得到了 XamlParseException。 DataGridTemplateColumn 应该添加到 DataGrid.Columns,而不是直接添加到 DataGrid。

<DataGrid.Columns>
    <DataGridTemplateColumn Header="user_buttons">
        <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <wpfDemos:GuestButtons/>
                </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>