我想爲一個標籤定義一個數據模板。這可能嗎?由於找不到有用的東西,所以我定義了一個控件模板並試圖讓事情順利進行。不幸的是,當我按如下方式綁定兩個項目時,即使具有給定參數的對象已成功綁定到後面代碼的標籤,我也會得到標籤不顯示任何內容。請幫忙!WPF標籤DataTemplate
<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>
什麼是表名和列名在綁定?你使用的是對象數據源綁定嗎? – 2011-03-17 11:32:39
嗨Mamta,你是對的。我將這兩個對象綁定爲屬性。 – 2011-03-17 11:37:22
請添加完整的xaml – 2011-03-17 11:57:45