WPF 标签数据模板

发布于 2024-10-24 03:37:36 字数 3635 浏览 1 评论 0原文

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

<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>

I am trying to define a datatemplate for a label. Is this possible? Since I could not find anything helpful to move forward I defined a control template and tried to get things working. Unfortunately when I bind two items as follows I get the label displaying nothing, even though an object with the given parameters has been successfully bound to the label from the code behind. Please help!

<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>

Edited: Full Xaml of the window.resources as requested

  <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音栖息无 2024-10-31 03:37:37

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

<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>

You need to change the binding as follows:

<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>

I tried the revised code with a simple class having the two properties and it worked successfully.

The ObjectDataProvider was referenced as follows:

 <ObjectDataProvider x:Key="data" ObjectType="{x:Type local:MyClass}">
 </ObjectDataProvider>
柳絮泡泡 2024-10-31 03:37:37

在这种情况下,我建议您创建自定义控件。会更优雅。

In this case I recommend you create custom control. It would be more elegant.

柒七 2024-10-31 03:37:37

恕我直言,标签的 DataContext 设置错误,因此没有显示值。

我的猜测是默认的 DataContext 适用于此处的 Label,并且它不包含 TableName 和 ColumnName 属性。

查看您的输出窗口并查找“错误 40,绑定 bla bla bla .....”这将为您提供更多信息。

请注意,损坏的数据绑定并不总是会产生异常,它们有时只会在输出窗口中显示警告。

您应该提供有关 Label 类以及如何将 dataContext 设置为包含上述属性的对象的更多信息。

您可能在某处缺少类似

MyCustomLabel.DataContext = MyCustomObjectCOntainingTheProperties;

的内容

IMHO, the Label's DataContext is set wrong, Hence the absence of value being displayed.

My guess is that the default DataContext applies for the Label here, and it does not contain the TableName and ColumnName properties.

have a look at your output window and look for "error 40, Binding bla bla bla ....." this will give you more info on this.

beware, broken dataBindings will not always produce an exception, they sometimes will only show a warning in the output window.

you should give more info on your Label class and how you set the dataContext to the object containing the mentioned properties.

you're probably missing something like

MyCustomLabel.DataContext = MyCustomObjectCOntainingTheProperties;

somewhere

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文