gpt4 book ai didi

c# - WPF 标签数据模板

转载 作者:太空宇宙 更新时间:2023-11-03 22:15:22 26 4
gpt4 key购买 nike

我正在尝试为标签定义数据模板。这可能吗?因为我找不到任何有助于前进的东西,所以我定义了一个控制模板并试图让事情正常进行。不幸的是,当我按如下方式绑定(bind)两个项目时,我得到的标签什么都不显示,即使具有给定参数的对象已从后面的代码成功绑定(bind)到标签。请帮忙!

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<Grid>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>

<TextBlock Background="Green" Text="{Binding TableName}" Grid.Column="0"></TextBlock>
<TextBlock Background="Red" Text="{Binding ColumnName}" Grid.Column="1"></TextBlock>

</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>

已编辑:根据要求对 window.resources 进行完整 Xaml

  <Window.Resources>

<Style x:Key="myLabelTemplate" TargetType="Label">
<!--<Setter Property="Background" Value="DarkKhaki"></Setter>
<Setter Property="Foreground" Value="White"></Setter>-->

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<Border>
<Grid>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>

<!-- <TextBlock Background="Green" Text="{Binding TableName}" Grid.Column="0"></TextBlock>
<TextBlock Background="Red" Text="{Binding ColumnName}" Grid.Column="1"></TextBlock>-->

</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>

</Style>


<!-- <ControlTemplate TargetType="Label" x:Key="tablelLabel">
<Grid>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="5" CornerRadius="5">
<Border.Background>
<SolidColorBrush Color="#50000000"></SolidColorBrush>
</Border.Background>
</Border>
<Grid>
<Grid.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="Transparent" Offset="0" />
<GradientStop Color="Black" Offset="1" />
<GradientStop Color="Transparent" Offset="1" />
</LinearGradientBrush>
</Grid.Background>
</Grid>
</Grid>

</ControlTemplate> -->

<ControlTemplate TargetType="Label" x:Key="generalLabel">
<Grid>
<Border BorderBrush="#34B7AEAE" BorderThickness="3" CornerRadius="5">
</Border>
</Grid>
</ControlTemplate>

</Window.Resources>

最佳答案

您需要按如下方式更改绑定(bind):

<TextBlock Background="Green" Text="{Binding Source={StaticResource data}, Path=TableName}" Grid.Column="0"/>
<TextBlock Background="Red" Text="{Binding Source={StaticResource data}, Path=ColumnName}" Grid.Column="1"></TextBlock>

我用一个具有这两个属性的简单类尝试了修改后的代码,它成功地工作了。

ObjectDataProvider 被引用如下:

 <ObjectDataProvider x:Key="data" ObjectType="{x:Type local:MyClass}">
</ObjectDataProvider>

关于c# - WPF 标签数据模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5338102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com