XAML - 通过 TextBlock 提供内容后按钮内容消失(DataGrid 的最后一行除外)
XAML - Button Content disappeared after giving content through a TextBlock (Except for the last Row of the DataGrid)
我将按钮内容用作 <Setter Property="Content" Value="Accept">
到那时一切都还好
但是,我不得不为内容放置一个 TextBlock,因为我想在按钮内容中使用 CharacterEllipsis。
从此Button的内容就消失了
奇怪的是,除了最后一行,它在所有行中都消失了。所有行的按钮代码都相同。因为我正在使用
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<--code for this column of every row-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这是代码片段和屏幕截图
<DataGrid.Columns>
<!--Item Desc column-->
<DataGridTemplateColumn Width="1.5*" CanUserResize="False" CanUserSort="False"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="20 0 0 0">
<Label Style="{StaticResource supplyCabinetColumnHeaderStyle}">
<Label.Content>
<TextBlock Text="Description" TextTrimming="CharacterEllipsis"/>
</Label.Content>
</Label>
<TextBlock Text="{Binding ItemDescription}" Margin="5 6 0 0" Style="{StaticResource supplyCabinetReadOnlyDataStyle}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Button Column-->
<DataGridTemplateColumn Width="0.7*" CanUserResize="False" CanUserSort="False" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Height="25" FontSize="12" FontFamily="Arial"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0 25 10 4" Padding="10 5 10 5">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content">
<Setter.Value>
<TextBlock Text="Accept" TextTrimming="CharacterEllipsis"/>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#DFE0E2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="{TemplateBinding Background}" BorderThickness="0.5" BorderBrush="Gray">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Button Content Issue
将 <TextBlock Text="Accept" TextTrimming="CharacterEllipsis"/>
包含到按钮的模板中而不是 ContentPresenter。 Setter 每个按钮仅使用一个 TextBlock 实例。但是TextBlock是FrameworkElement,不能有多个Parent(不能多处显示)。如果将其移至模板,每个 Button 将获得自己的 TextBlock 实例。
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content" Value="Accept">
<Setter Property="Background" Value="#DFE0E2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="{TemplateBinding Background}" BorderThickness="0.5" BorderBrush="Gray">
<TextBlock Text="{TemplateBinding Content}"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
我将按钮内容用作 <Setter Property="Content" Value="Accept">
到那时一切都还好
但是,我不得不为内容放置一个 TextBlock,因为我想在按钮内容中使用 CharacterEllipsis。
从此Button的内容就消失了
奇怪的是,除了最后一行,它在所有行中都消失了。所有行的按钮代码都相同。因为我正在使用
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<--code for this column of every row-->
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
这是代码片段和屏幕截图
<DataGrid.Columns>
<!--Item Desc column-->
<DataGridTemplateColumn Width="1.5*" CanUserResize="False" CanUserSort="False"
IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="20 0 0 0">
<Label Style="{StaticResource supplyCabinetColumnHeaderStyle}">
<Label.Content>
<TextBlock Text="Description" TextTrimming="CharacterEllipsis"/>
</Label.Content>
</Label>
<TextBlock Text="{Binding ItemDescription}" Margin="5 6 0 0" Style="{StaticResource supplyCabinetReadOnlyDataStyle}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!--Button Column-->
<DataGridTemplateColumn Width="0.7*" CanUserResize="False" CanUserSort="False" IsReadOnly="True" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Height="25" FontSize="12" FontFamily="Arial"
HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0 25 10 4" Padding="10 5 10 5">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content">
<Setter.Value>
<TextBlock Text="Accept" TextTrimming="CharacterEllipsis"/>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="#DFE0E2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="{TemplateBinding Background}" BorderThickness="0.5" BorderBrush="Gray">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}"
Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Button Content Issue
将 <TextBlock Text="Accept" TextTrimming="CharacterEllipsis"/>
包含到按钮的模板中而不是 ContentPresenter。 Setter 每个按钮仅使用一个 TextBlock 实例。但是TextBlock是FrameworkElement,不能有多个Parent(不能多处显示)。如果将其移至模板,每个 Button 将获得自己的 TextBlock 实例。
<Button.Style>
<Style TargetType="Button">
<Setter Property="Content" Value="Accept">
<Setter Property="Background" Value="#DFE0E2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="{TemplateBinding Background}" BorderThickness="0.5" BorderBrush="Gray">
<TextBlock Text="{TemplateBinding Content}"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>