WPF单击按钮后在customcontrol中更改属性 [英] WPF Change a property in a customcontrol after a button click

查看:191
本文介绍了WPF单击按钮后在customcontrol中更改属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在链接。也许我表现得不好。
这很简单,我想在单击外部的Boutton之后更改usercontrol或CustomControl中的属性...

I posted a question in this link. maybe I'm not well expressed. It's very simple, I want to change a property in a usercontrol or CustomControl after a click on a Boutton outside...

customcontrol的代码是如下:

The code of the customcontrol is as follows :

    <Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border x:Name="container" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">

                </Border>
                <ControlTemplate.Triggers>
                    <Trigger  Property="Hidden" Value="true">
                        <Setter Property="BorderBrush" Value="Blue"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>



</Style>

 public class CustomControl1 : Control
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }



    public bool Hidden
    {
        get { return (bool)GetValue(HiddenProperty); }
        set { SetValue(HiddenProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Hidder.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty HiddenProperty =
        DependencyProperty.Register("Hidden", typeof(bool), typeof(CustomControl1), new PropertyMetadata(false));


}

还有一个简单的测试窗口

And a simple window for test

<Window x:Class="WpfTestCustomControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomBorder;assembly=WpfCustomBorder"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="400"/>
        <ColumnDefinition Width="70"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <local:CustomControl1 x:Name="cc" BorderBrush="Red" BorderThickness="3" Margin="10" Grid.RowSpan="2"/>
    <Button Grid.Column="1"  Content="Ok"  Margin="5" Click="Button_Click"/>
</Grid>

namespace WpfTestCustomControl
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        cc.Hidden = true;
    }
}
}

属性隐藏为自定义控件内的依赖项属性。
当我单击主窗口中的按钮时,我想将hidden属性更改为true。这必须在自定义控件内触发触发器,以将borderbrush更改为蓝色颜色。虽然什么也没发生。

The property "Hidden" is a dependency property inside the custom control. When i click on the button in mainwindow i want to change the hidden property to true. this must fire the trigger inside the custom control to change borderbrush to "blue" color. While nothing happen.

是否缺少某些东西或不是正确的方法?
预先感谢。.

Is there something missing or is not the right way to do it ? Thanks in advance..

推荐答案

不要硬设置 BorderBrush = Red 在控件的声明中,它优先于任何触发器的设置器。

Don't hard-set BorderBrush="Red" in your Control's declaration, it's prioritary over any trigger's setter.

您可能要检查msdn的相关性属性值优先级

You might want to check msdn's Dependency Property Value Precedence

这篇关于WPF单击按钮后在customcontrol中更改属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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