Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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# 更改窗口中的控件属性。代码隐藏中的资源_C#_Wpf_Controltemplate - Fatal编程技术网

C# 更改窗口中的控件属性。代码隐藏中的资源

C# 更改窗口中的控件属性。代码隐藏中的资源,c#,wpf,controltemplate,C#,Wpf,Controltemplate,我有一个自定义样式的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" AllowsTranspare

我有一个自定义样式的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>
private void ChangeBackground()
{
     BORDERCONTROL.Background = Brushes.Yellow;
}
但我无法使用此方法访问此控件

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

谢谢您的帮助。

试试这个:

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

如果使用
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;
}

私有void ChangeBackground()
{
myWindow.Background=画笔.Yellow;
}

这将为您提供所需的边框控件,并更改其颜色。但不要尝试访问窗口构造函数中的边框,因为它将始终为空。

感谢您的帮助。我认为@AnjumSKhan解决方案更适合我。
private void Button_Click(object sender, RoutedEventArgs e)
    {
        Border b = (Border)this.Template.FindName("BORDERCONTROL", this);
        b.Background = new SolidColorBrush(Colors.Yellow);
    }