如何在用户控件内公开自定义控件属性?

发布于 2024-09-15 04:47:22 字数 2093 浏览 0 评论 0原文

我有一个名为 TextBoxWithLabelAndUnits 的自定义控件。从名称上可以看出,它是一个 TextBox,前面有一个 Label,后面有一个 Label,这样自定义控件看起来就像像这样:

       -----------------
Label: |               | units
       -----------------

我公开了几个依赖属性来配置控件,例如:

LabelWidth
LabelText
UnitText
TextBoxWidth

现在我有一个名为 LatLong 的用户控件,我将其用于纬度/经度输入。 XAML 如下所示:

<UserControl ...>
    <StackPanel>
        <TextBoxWithLabelAndUnits LabelText="Latitude:"
                                  UnitText="degrees"
        />
        <TextBoxWithLabelAndUnits LabelText="Longitude:"
                                  UnitText="degrees" 
        />
    </StackPanel>
</UserControl>

它将创建一个如下所示的用户控件:

           -----------------
Latitude:  |               | degrees
           -----------------
           -----------------
Longitude: |               | degrees
           -----------------

现在我想在项目中使用我的用户控件。但是,我希望用户控件公开属性,以便在不喜欢默认设置时可以更改标签。我可以将每个属性公开为新的依赖项属性,如下所示:

LatitudeLabelWidth
LatitudeLabelText
LatitudeUnitText
LatitudeTextBoxWidth

LongitudeLabelWidth
LongitudeLabelText
LongitudeUnitText
LongitudeTextBoxWidth

使用 LatLong 控件的 XAML 将如下所示:

<Window ...>
    <LatLong LatitudeLabelText="Latitude (in degrees)"
             LatitudeUnitText=""
             LongitudeLabelText="Longitude (in degrees)"
             LongitudeUnitText=""
    />
</Window>

但这似乎需要大量工作,因为我必须重新-公开每个实例的每个依赖属性。我想知道是否有一种更简单的方法来公开 TextBoxWithLabelAndUnits 实例本身,这样我就可以直接在其上编辑属性并使用如下所示的 XAML:

<Window ...>
    <LatLong Latitude.LabelText="Latitude (in degrees)"
             Latitude.UnitText=""
             Longitude.LabelText="Longitude (in degrees)"
             Longitude.UnitText=""
    />
</Window>

所以换句话说,而不是公开属性在用户控件内的每个自定义控件上,我公开自定义控件本身并使用点表示法访问所有属性。

有人知道这是否可以在 WPF 中实现吗?

I have a custom control called TextBoxWithLabelAndUnits. From the name, you can tell that it's a TextBox that has a Label before it and a Label after it, so that the custom control looks like this:

       -----------------
Label: |               | units
       -----------------

I expose several dependency properties to configure the control, such as:

LabelWidth
LabelText
UnitText
TextBoxWidth

Now I have a user control called LatLong that I'm using for Latitude/Longitude input. The XAML looks like this:

<UserControl ...>
    <StackPanel>
        <TextBoxWithLabelAndUnits LabelText="Latitude:"
                                  UnitText="degrees"
        />
        <TextBoxWithLabelAndUnits LabelText="Longitude:"
                                  UnitText="degrees" 
        />
    </StackPanel>
</UserControl>

It will create a user control that looks like this:

           -----------------
Latitude:  |               | degrees
           -----------------
           -----------------
Longitude: |               | degrees
           -----------------

Now I want to use my user control in a project. However, I'd like the user control to expose properties so I can change the labels if I don't like the default settings. I can expose each one as a new dependency property to look like this:

LatitudeLabelWidth
LatitudeLabelText
LatitudeUnitText
LatitudeTextBoxWidth

LongitudeLabelWidth
LongitudeLabelText
LongitudeUnitText
LongitudeTextBoxWidth

And the XAML for something that uses a LatLong control will look like this:

<Window ...>
    <LatLong LatitudeLabelText="Latitude (in degrees)"
             LatitudeUnitText=""
             LongitudeLabelText="Longitude (in degrees)"
             LongitudeUnitText=""
    />
</Window>

But that seems like a lot of work since I have to re-expose every dependency property for each instance. I was wondering if there was an easier way that exposes the TextBoxWithLabelAndUnits instance itself, so I can edit the properties directly on it and use XAML that looks like this:

<Window ...>
    <LatLong Latitude.LabelText="Latitude (in degrees)"
             Latitude.UnitText=""
             Longitude.LabelText="Longitude (in degrees)"
             Longitude.UnitText=""
    />
</Window>

So in other words, rather than exposing the properties on each custom control inside the user control, I expose the custom control itself and get access to all the properties using dot notation.

Does anybody know if that's possible to do in WPF?

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

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

发布评论

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

评论(1

乱了心跳 2024-09-22 04:47:22

当然 - 为什么你不直接给你的 LatLong 这两个属性呢?

public TextBoxWithLabelAndUnits LatitudeControl
{
    get { return latitude; }
}

public TextBoxWithLabelAndUnits LongitudeControl
{
    get { return longitude; }
}

然后可以通过代码直接访问它们。

Sure -- why don't you just give your LatLong these two properties?

public TextBoxWithLabelAndUnits LatitudeControl
{
    get { return latitude; }
}

public TextBoxWithLabelAndUnits LongitudeControl
{
    get { return longitude; }
}

Then they will be directly accessible through code.

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