首页 文章

ControlTemplate中的UWP TemplateBinding为固定值

提问于
浏览
0

我想将我的Controltemplate中的属性(myHeight)绑定到父级 . 以下是我目前的代码 .

Resource dict

<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中有这个,因为我需要在它上面运行一些操作 . 这无法完全加载!


我也尝试了以下方法

Resource dict

<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 回答

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

相关问题