从设置的StaticResource的RowDefinition高度 [英] Setting the RowDefinition Height from StaticResource

查看:106
本文介绍了从设置的StaticResource的RowDefinition高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF的风格我已经定义了一个标准的网格行的高度,我想申请好几个地方,像这样:

In my WPF style I have defined a standard grid row height I'd like to apply to several places like so:

<system:Double x:Key="TableRowHeight">22</system:Double>



然而,当我想申请这个,像这样不工作:

However it does not work when I'd like to apply this like so:

<RowDefinition Height="{StaticResource TableRowHeight}"/>



相反,我需要建立一个完整的风格,如:

Instead I need to create a complete style like:

<Style x:Key="GridTableRow" TargetType="{x:Type RowDefinition}">
    <!--<Setter Property="Height" Value="{StaticResource TableRowHeight}"/>-->
    <Setter Property="Height" Value="22"/>
</Style>



从上注释掉行试图内的样式定义不引用数字常量可以看出无论是工作,但硬编码的价值呢。

As can be seen from the commented out line trying to reference the numeric constant within the Style definition does not work either, but the "hardcoded" value does.

现在我看着它,我想这是因为随着高度属性相关联的类型是GridLength它不知何故从另一个正在添加的资源时,不管理到自动转换的double值...

Now I've looked it up and I guess it is because the type associated with the Height property is GridLength and it somehow does not manage to automatically cast the double value when comming from another resource...

的问题是,似乎没有要创建一个GridLength对象的一种方式从XAML。价值属性为只读。因此,像这样不工作之一:

The problem is that there does not seem to be a way of creating a GridLength object from XAML. The Value propery is readonly. So something like this does not work either:

<Style x:Key="GridTableRow" TargetType="{x:Type RowDefinition}">
    <Setter Property="Height">
        <Setter.Value>
            <GridLength Value="{StaticResource TableRowHeight}"/>
        </Setter.Value>
    </Setter>
</Style>



有没有办法得到这个工作,或者我应该忘记使用恒定的,只是使用有了不断的硬编码值的RowDefinition风格?

Is there a way to get this to work, or should I just forget using that constant and just use the RowDefinition style with the hardcoded value in place of the constant?

推荐答案

在硬编码的价值观,在XAML处理器会最多可以从字符串转换它必要的类型转换器。即使你的 TableRowHeight 资源使用 DoubleConverter 创建。 GridLength GridLengthConverter

When you "hard code" values, the XAML processor looks up a converter that can convert it from string to the necessary type. Even your TableRowHeight resource is using DoubleConverter to be created. GridLength uses GridLengthConverter.

因此,有没有自动投/转换在编译器中发生的一切 - WPF需要明确地查找一个类,并调用转换方法。在静态资源的情况下,它跳过此步骤。

So there is no automatic cast / conversion happening in the compiler -- WPF needs to explicitly look up a class and call a convert method. In the case of StaticResource, it skips this step.

绑定确实使用类型转换器的,所以下面的的工作,正如所预期:

Bindings do use type converters though, so the following would work as you expect:

<RowDefinition Height="{Binding Source={StaticResource TableRowHeight}}" />

这工作,因为 GridLengthConverter 知道如何转换从双击。在你的情况,这不应该是必要的,但。如果您在您初始化以同样的方式初始化 GridLength 资源双击(标签内),字符串转换将是所谓分配资源之前:

This works because GridLengthConverter knows how to convert from Double. In your case, this should not be necessary, though. If you initialize a GridLength resource in the same way you initialized Double (inside the tags), the string conversion will be called before the resource is assigned:

<GridLength x:Key="TableRowHeight">22</GridLength>



然后,你将能够直接调用的资源。

Then you would be able to call the resource directly.

这篇关于从设置的StaticResource的RowDefinition高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆