Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在运行时更改在XAML中设计的自定义按钮的背景色?_C#_Windows Phone 7 - Fatal编程技术网

C# 如何在运行时更改在XAML中设计的自定义按钮的背景色?

C# 如何在运行时更改在XAML中设计的自定义按钮的背景色?,c#,windows-phone-7,C#,Windows Phone 7,我使用expression blend制作了一个自定义按钮,并将其粘贴到我的xaml代码中 <phone:PhoneApplicationPage.Resources> <Style x:Key="ButtonStyle1" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <Control

我使用expression blend制作了一个自定义按钮,并将其粘贴到我的xaml代码中

<phone:PhoneApplicationPage.Resources>
 <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="Disabled"/>
                                <VisualState x:Name="MouseOver"/>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle RadiusY="21" RadiusX="20" Stroke="White"/>
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
 </Style>
</phone:PhoneApplicationPage.Resources>
但我不能这样做,按钮上没有反映任何变化。 有办法吗。 我知道我必须改变矩形的颜色。但我不知道怎么做。 我试了很多。
谢谢。

那么,哪些属性应该影响哪些属性呢?您为按钮创建了自定义控件模板,这意味着您可以决定如何绘制按钮。如果查看模板,您的按钮由一个
网格
和两个子控件组成:一个
矩形
和一个
ContentPresenter

现在,当你说:

var btn = new Button
{
    Background = new SolidColorBrush(Color.Blue);
};
什么应该变成蓝色?
网格
矩形
控件<代码>内容演示者?这个问题原则上不可回答,您需要决定
ControlTemplate
中的哪个控件继承父控件的属性

换句话说:
按钮的属性通过
模板绑定
向下传输到
控制模板
中的控件。因此,如果您想将
按钮.Background
的含义转换为
矩形.Background
,您可以将模板更改为:

<Rectangle RadiusY="21" RadiusX="20" Stroke="White"
            Fill="{TemplateBinding Background}"/>


现在你得到了你想要的东西。当您设置
按钮.Background
时,您只需将其传输到目标子控件,因为可视化树中没有
按钮
,它将被其控件模板替换。

我不确定,但是否有类似UseVisualStyleBackground的属性?或者看看这个链接,如果它可以帮助抱歉Jax,但我仍然没有任何线索。这有帮助吗?谢谢托尼,对不起,若我并没有正确地解释它,实际上我想改变矩形的背景。
<Rectangle RadiusY="21" RadiusX="20" Stroke="White"
            Fill="{TemplateBinding Background}"/>