【问题标题】:Change control property located in Window.Resources from code-behind从代码隐藏更改位于 Window.Resources 中的控件属性
【发布时间】:2015-11-24 10:48:51
【问题描述】:

我有一个自定义样式的 WPF 窗口,如下所示:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" AllowsTransparency="True" WindowStyle="None" Width="525" Style="{DynamicResource CustomWindowStyle}">
<Window.Resources>
    <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="Black" BorderThickness="7" Background="{x:Null}" MouseMove="WindowMouseMove" MouseDown="WindowMouseDown">
                            <Grid Background="Transparent" DockPanel.Dock="Top">

                            <!--WPF Control of interest-->
                            <Border x:Name="BORDERCONTROL" HorizontalAlignment="Left" Margin="10,10,0,0" Width="20" Height="20" Background="Black" />
                            <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>

</Grid>

我想像这样以编程方式更改 BORDERCONTROL 背景:

private void ChangeBackground()
{
     BORDERCONTROL.Background = Brushes.Yellow;
}

但我无法使用此方法访问此控件。

有没有一种简单的方法可以从代码隐藏中更改位于控件模板中的控件属性?

感谢您的帮助。

【问题讨论】:

    标签: c# wpf controltemplate


    【解决方案1】:
    private void Button_Click(object sender, RoutedEventArgs e)
        {
            Border b = (Border)this.Template.FindName("BORDERCONTROL", this);
            b.Background = new SolidColorBrush(Colors.Yellow);
        }
    

    这将为您提供所需的边框控件,并更改其颜色。但不要尝试在 Window Constructor 中访问 Border,因为它总是为 null。

    【讨论】:

      【解决方案2】:

      试试这个:

      var bor = (Border)Template.FindName("BORDERCONTROL", this);
      bor.Background = Brushes.Yellow;
      

      【讨论】:

        【解决方案3】:

        如果您使用TemplateBinding,您可以绑定到窗口的Background 属性并更改它:

        <Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Window}">
                            <Border BorderBrush="Black" BorderThickness="7" Background="{x:Null}" 
                                    MouseMove="WindowMouseMove" MouseDown="WindowMouseDown">
                                    <Grid Background="Transparent" DockPanel.Dock="Top">
        
                                    <!--WPF Control of interest-->
                                    <Border x:Name="BORDERCONTROL" HorizontalAlignment="Left" 
                                            Margin="10,10,0,0" Width="20" Height="20" 
                                            Background="{TemplateBinding Background}" />
                                    <AdornerDecorator>
                                    <ContentPresenter/>
                                </AdornerDecorator>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        
        private void ChangeBackground()
        {
             myWindow.Background = Brushes.Yellow;
        }
        

        【讨论】:

        • 感谢您的帮助。我认为@AnjumSKhan 解决方案对我来说更好。
        猜你喜欢
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 2011-01-30
        • 2015-06-06
        • 1970-01-01
        • 2016-09-05
        • 2012-02-15
        • 1970-01-01
        相关资源
        最近更新 更多