将ControlTemplate中的UWP TemplateBinding固定为固定值

时间:2018-12-10 07:50:53

标签: c# xaml uwp uwp-xaml

我想将ControlTemplate中的属性(myHeight)绑定到父项。以下是我到目前为止的代码。

资源字典

<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

[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中使用它,因为我需要对其进行一些操作。无法完全加载!


我也尝试了以下方法

资源字典

<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

[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),将不胜感激。

1 个答案:

答案 0 :(得分:1)

第一件事是TemplateBinding应该绑定依赖项属性,然后编写无法绑定到Height的静态文件。

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

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

    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来获取资源。

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

    <local:TestingControl>
        <local:TestingControl.Resources>
            <x:Double x:Key="myHeight">100</x:Double>
        </local:TestingControl.Resources>
    </local:TestingControl>