是否可以在 ControlTemplate 中包含 Xaml 资源?

发布于 2024-08-04 20:12:48 字数 259 浏览 1 评论 0原文

我正在开发一个 Silverlight 模板化控件(即它派生自 Control 并在 generic.xaml 中将其外观实现为 ControlTemplate),并且在我的绑定之一中遇到了对 ValueConverter 的需求。通常,我只是在使用的控件的资源中创建 ValueConverter 的实例,但我不知道如何使用 ControlTemplate 来做到这一点。如果可能的话,我希望将此实现全部保留在 Xaml 中,但我想我可以重写 OnApplyTemplate 并手动创建与转换器的绑定。

I'm working on a Silverlight templated control (i.e. it derives from Control and has its look implemented as a ControlTemplate in generic.xaml) and came across the need for a ValueConverter in one of my bindings. Normally I'd just create an instance of the ValueConverter in the Resources of the control working with but I can't figure out how to do that with a ControlTemplate. I'd like to keep this implementation all in Xaml if possible, but I suppose I could override OnApplyTemplate and manually create the binding with a converter.

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

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

发布评论

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

评论(1

梦途 2024-08-11 20:12:48

这在 Silverlight 3 中确实有效。您只需在控件模板中添加一个资源部分,并将 ValueConverter 添加到模板的资源中:

<ControlTemplate TargetType="controls:MyControl">
    <Grid Margin="8,40,8,20" x:Name="peopleListContainer">        
        <Grid.Resources>
            <controls:MyValueConverter x:Key="converter" />
        </Grid.Resources>
        <!-- ....

但是,ValueConverter 在控件模板中经常被误用。您可能想改用 TypeConverter。 Bryant Likes 写了一篇很好的文章,讨论 此场景中 ValueConverter 和 TypeConverter 之间的区别

This does work in Silverlight 3. You just need to add a resources section inside your Control Template, and add the ValueConverter to the template's resources:

<ControlTemplate TargetType="controls:MyControl">
    <Grid Margin="8,40,8,20" x:Name="peopleListContainer">        
        <Grid.Resources>
            <controls:MyValueConverter x:Key="converter" />
        </Grid.Resources>
        <!-- ....

However, ValueConverters are often misused in Control Templates. You may want to use a TypeConverter instead. Bryant Likes wrote a good article discussing the difference between ValueConverter and TypeConverter in this scenario.

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