WPF控件模板

发布于 2024-07-27 17:58:24 字数 884 浏览 3 评论 0原文

我有一个非常简单的案例,我认为使用模板会受益(但我不确定,这就是我问的原因)。 我见过的所有模板示例要么假设了比我更多的知识,要么过于具体,对像我这样的新手没有多大用处,要么包含大量辅助内容,使得很难识别模板的一部分。

这是设置: 我有两个并排的标签,第一个标签填充了字段的名称,第二个标签填充了字段的值。

这是我当前在我的应用程序中使用的 XAML(很多很多次):

<StackPanel Style="{StaticResource horizontalStackerStyle}">
    <Label Style="{StaticResource labelStyle}">Field One:</Label>
    <Label Style="{StaticResource valueStyle}" Name="field1" 
        Content="{Binding dataObject.field1}" />
</StackPanel>

我想创建一个模板,以便我可以像这样编写 XAML:

<CustomControlOrWhatever 
    FieldName="Field One:" 
    FieldValue="{Binding dataObject.field1}"/>

我感觉我可以使用某种模板来做到这一点。 这样做的好处之一是我不需要一遍又一遍地指定样式。 我对么? 我该怎么做?

提前致谢!

更新:

仍然没有找到这个问题的答案。 我使用依赖属性选择了一个可能的解决方案,并尝试询问

需要明确的是:这只需要是单向绑定,并且值每隔几秒更新一次。

I have a very simple case that I think would benefit from using templates (but I'm not sure, which is why I'm asking). All the templating examples I've seen either assume more knowledge than I have, are too specific to be of much use to a total newb like myself, or contain lots of ancillary stuff that makes it hard to identify what's part of the template.

Here's the setup:
I have two labels side-by-side, with the first label populated with the name of a field, and the second label populated with the value of the field.

Here is the XAML I currently have in my app (many, many times):

<StackPanel Style="{StaticResource horizontalStackerStyle}">
    <Label Style="{StaticResource labelStyle}">Field One:</Label>
    <Label Style="{StaticResource valueStyle}" Name="field1" 
        Content="{Binding dataObject.field1}" />
</StackPanel>

I would like to create a template such that I could write XAML like this:

<CustomControlOrWhatever 
    FieldName="Field One:" 
    FieldValue="{Binding dataObject.field1}"/>

I have a feeling I can do this with some kind of template. One benefit of which would be that I don't need to keep specifying the styles over and over. Am I correct? How would I do this?

Thanks in advance!

UPDATE:

Still haven't found an answer to this. I chose a possible solution using Dependency Properties, and tried to ask a clarifying question here. Well, the first responder said that I don't actually need to clutter up my code behind with DP nonsense, so I changed it again--and it still doesn't work. Can anyone come up with a working solution? This seems like it should be so simple.

Just to be clear: this only needs to be one-way binding with values updated every few seconds.

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

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

发布评论

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

评论(3

顾冷 2024-08-03 17:58:24

您要求的基本上是用户控件。

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SomeNameSpace.SomeControlName">
    <Grid x:Name="LayoutRoot">
     <StackPanel Style="{StaticResource horizontalStackerStyle}">
      <Label Style="{StaticResource labelStyle}" x:Name="FieldNameLbl"></Label>
      <Label Style="{StaticResource valueStyle}" x:Name="ValueLbl">
      </StackPanel>
    </Grid>
</UserControl>

在后面的代码中,您需要公开两个用于设置控件值的属性。

public string FieldName
{
    get { return FieldNameLbl.Text; }
    set { FieldNameLbl.Text = value; }
}

public string FieldValue
{
    get { return ValueLbl.Text; }
    set { ValueLbl.Text = value; }
}

然后要调用它,您可以将其与其余声明一起放在窗口/页面的顶部:

xmlns:Controls="clr-namespace:SomeNameSpace" 

然后您可以将控件插入到窗口/页面中,如下所示:

<Controls:NameOfYourControl FieldName="Field One:" FieldValue="{Binding dataObject.field1}"/>

What you're asking for is basically a user control.

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SomeNameSpace.SomeControlName">
    <Grid x:Name="LayoutRoot">
     <StackPanel Style="{StaticResource horizontalStackerStyle}">
      <Label Style="{StaticResource labelStyle}" x:Name="FieldNameLbl"></Label>
      <Label Style="{StaticResource valueStyle}" x:Name="ValueLbl">
      </StackPanel>
    </Grid>
</UserControl>

In the code behind, you'd need to expose two properties that would set the value of the controls.

public string FieldName
{
    get { return FieldNameLbl.Text; }
    set { FieldNameLbl.Text = value; }
}

public string FieldValue
{
    get { return ValueLbl.Text; }
    set { ValueLbl.Text = value; }
}

And then to call that you can put this at the top of your window/page with the rest of your declarations:

xmlns:Controls="clr-namespace:SomeNameSpace" 

and then you can insert the control into your window/page like this:

<Controls:NameOfYourControl FieldName="Field One:" FieldValue="{Binding dataObject.field1}"/>
海未深 2024-08-03 17:58:24

您可以创建一个名为 FieldControlUserControl,并为 FieldNameFieldValue 定义支持(自动)属性。 (普通属性就可以了,只要您只需要绑定一次,这可能就是这种情况。)

XAML 代码可能如下所示:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="foo.bar">
    <StackPanel Style="{StaticResource horizontalStackerStyle}">
        <Label Style="{StaticResource labelStyle}" Content="{Binding Path=FieldName, Mode=OneTime, StringFormat='{0}: '}"/>
        <Label Style="{StaticResource valueStyle}" Content="{Binding Path=FieldValue, Mode=OneTime}" />
    </StackPanel>
</UserControl>

希望有所帮助。

You could create a UserControl called FieldControl and define backing (automatic) properties for FieldName and FieldValue. (Normal properties would be fine, so long as you only need to bind once, which is probably the case.)

The XAML code might look like:

<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             x:Class="foo.bar">
    <StackPanel Style="{StaticResource horizontalStackerStyle}">
        <Label Style="{StaticResource labelStyle}" Content="{Binding Path=FieldName, Mode=OneTime, StringFormat='{0}: '}"/>
        <Label Style="{StaticResource valueStyle}" Content="{Binding Path=FieldValue, Mode=OneTime}" />
    </StackPanel>
</UserControl>

Hope that helps.

大海や 2024-08-03 17:58:24

您想要做的事情类似于关于通过指定路径的简单方法将图像放在按钮上的讨论,例如

总结:

  • 一种明显的方法是创建一个包含两个标签并公开两个属性的 UserControl。 这里没有太多模板。

  • 最 WPFish 的解决方案似乎是在其中一个标签上使用两个附加属性(例如值),并为其提供一个包含另一个标签(描述)的控件模板。 在模板中,您将每个标签文本绑定到相应的附加属性值。

What you want to do is similar to the discussion about putting images on a button with a simple way of specifying the path, like <Button MyImage="foo.jpg" />. Follow this article for the actual details.

To summarize:

  • One obvious way would be to create an UserControl containing your two labels and exposing the two properties. Not much templating here.

  • The most WPFish solution seems to be to use two Attached Properties on one of the labels (say the value), and provide a control template for it that includes the other label (the description). In the template, you bind each label text to the corresponding attached property value.

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