首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UWP TemplateBinding在ControlTemplate中的固定值

UWP TemplateBinding在ControlTemplate中的固定值
EN

Stack Overflow用户
提问于 2018-12-10 07:50:52
回答 1查看 208关注 0票数 0

我希望将控件模板中的属性(myHeight)绑定到父对象。下面是到目前为止我的代码。

资源dict

代码语言:javascript
复制
<Style TargetType="local2:TestingControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local2:TestingControl">
                <Border
                    Height="{TemplateBinding myHeight}"
                    Background="Green"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <StackPanel>                            
                        <ContentPresenter Content="{TemplateBinding Content}"/>
                    </StackPanel>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TestingControl.cs

代码语言:javascript
复制
[ContentProperty(Name = "Content")]
public sealed class TestingControl : Control
{
    public TestingControl()
    {
        this.DefaultStyleKey = typeof(TestingControl);
    }

    public static readonly double myHeight = (double)100;

    public object Content
    {
        get { return (string)GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    public static readonly DependencyProperty ContentProperty =
        DependencyProperty.Register("Content", typeof(string), typeof(TestingControl), new PropertyMetadata(string.Empty));
}

我想要绑定的是myHeight。我想在.cs中使用这个,因为我需要对它运行一些操作。这完全无法加载!

我还尝试了以下方法

资源dict

代码语言:javascript
复制
<x:Double x:Key="myHeight">100</x:Double>
<Style TargetType="local2:TestingControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local2:TestingControl">
                <Border
                    Height="{ThemeResource myHeight}"
                    Background="Green"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">

                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

TestingControl.cs

代码语言:javascript
复制
[ContentProperty(Name = "Content")]
public sealed class TestingControl : Control
{
    public TestingControl()
    {
        this.DefaultStyleKey = typeof(TestingControl);

        var x  = (double)Resources["myHeight"];
    }

    public object Content
    {
        get { return (string)GetValue(ContentProperty); }
        set { SetValue(ContentProperty, value); }
    }

    public static readonly DependencyProperty ContentProperty =
        DependencyProperty.Register("Content", typeof(string), typeof(TestingControl), new PropertyMetadata(string.Empty));
}

第二种方法的问题是,在读取.cs代码中的属性时,var x = (double)Resources["myHeight"];会出现异常。

决心(最好是两者兼而有之,因为我只是试图学习UWP)将是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-10 08:09:45

首先,TemplateBinding应该绑定依赖项属性,然后编写不能绑定到高度的静态字段。

第二件事是ThemeResource会找到主题,但是您定义了一个静态源。

代码语言:javascript
复制
<x:Double x:Key="myHeight">100</x:Double>
<Style TargetType="local2:TestingControl" >
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local2:TestingControl">
                <Border
                    Height="{StaticResource myHeight}"
                    Background="Green"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">

                    <ContentPresenter Content="{TemplateBinding Content}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

第三件事是,您首先获得资源,但资源在OnApplyTemplate之后。

您应该将获取资源的代码移动到OnApplyTemplate。

代码语言:javascript
复制
    protected override void OnApplyTemplate()
    {
        try
        {
            // find something in TestingControl.Resources
            var x = Resources["myHeight"];
        }
        catch (Exception e)
        {

        }

        try
        {
            // find something in App.Resources
            var x = App.Current.Resources["myHeight"];
        }
        catch (Exception e)
        {

        }

        base.OnApplyTemplate();
    }

如果您的资源是用App.xaml编写的,那么您应该使用App.Current.Resources来获取资源。

如果要在海关控制中获取资源,则应将资源添加到控件中。

代码语言:javascript
复制
    <local:TestingControl>
        <local:TestingControl.Resources>
            <x:Double x:Key="myHeight">100</x:Double>
        </local:TestingControl.Resources>
    </local:TestingControl>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53701460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档