我有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
行内部没有任何内容,即使我的button
内的UserControl
在设计器中可见。稍后这将是动态的,这就是我不仅仅在我提到的行内键入按钮的原因,因为我将为客人,管理员和客户提供按钮列表。再说一次,如何让XAML UserControl
的内容出现在DataGrid
的一行内?
我添加了
HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch" VerticalContentAlignment="Stretch"
在DataGrid
内,但问题仍然存在。
代码编译并运行没有任何错误。
我创建了一个全新的解决方案并从this MSDN article复制粘贴了XAML代码,没有任何改变。即使我运行项目,它也不会给出结果。我在Visual Studio 2015上,我会重新安装吗? :d
答案 0 :(得分:1)
我复制了样本并使其工作我修复了两个问题:
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>