在WPF中,为什么使用TemplateBinding时Rectangle.Fill属性似乎不起作用?

wil*_*lem 1 wpf templatebinding

我无法弄清楚为什么这个XAML代码不起作用.使用TemplateBinding(见下文)时,设置背景颜色.但是当我使用普通的颜色字符串(即"红色")时,它工作正常.

<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
    <Grid>
        <Rectangle>
            <Rectangle.Fill>
                <SolidColorBrush Color="{TemplateBinding Background}"></SolidColorBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

然而,当我以这种方式使用TemplateBinding时,它工作正常......

<ControlTemplate x:Key="InstanceButtonTemplate" TargetType="{x:Type Control}">
    <Grid>
        <Rectangle Fill="{TemplateBinding Background}"></Rectangle>
    </Grid>
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

编辑:为了澄清,我打算将其扩展为使用渐变画笔,这就是为什么我需要能够使用XAML而不是普通字符串分配给Rectangle.Fill属性.

Arc*_*rus 5

那是因为Color与Background有不同的类型

背景是画笔,颜色是..好颜色..您可以使用IValueConverter将画笔转换为颜色..

HTH