通过XAML访问`DependencyProperty`并进行评估? [英] Access `DependencyProperty` via XAML and evaluate it?

查看:106
本文介绍了通过XAML访问`DependencyProperty`并进行评估?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚为我的自定义按钮创建了DependencyProperty. 该属性称为IsChecked. 如果IsChecked == true,我的按钮应将其背景颜色更改为其他颜色,并保持该颜色直到IsChecked == false.

I just created a DependencyProperty for my custom button. The property is called IsChecked. If IsChecked == true my button shall change its background color to something else and keep this color until IsChecked == false.

这是我的DependencyProperty:

public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(bool), typeof(MainMenuButton), new PropertyMetadata(false));

    public bool IsChecked
    {
        get { return (bool)GetValue(IsCheckedProperty); }
        set { SetValue(IsCheckedProperty, value); }
    }

下一步,我为此按钮输入ControlTemplate

Next I have a ControlTemplate for this button:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<ControlTemplate x:Key="MainMenuButtonStyle" TargetType="{x:Type UserControl}">
    <ControlTemplate.Resources>
                /* Some storyboards */
    </ControlTemplate.Resources>
    <Grid x:Name="grid">
        <Grid.Background>
            <SolidColorBrush Color="{DynamicResource MainUI_MainMenuButtonBackground}"/>
        </Grid.Background>
    </Grid>
    <ControlTemplate.Triggers>
                /* Some triggers */
    </ControlTemplate.Triggers>
</ControlTemplate>

我的问题是,现在我该如何访问IsChecked,并根据其当前值更改grid的背景?之前还没有尝试过,但是完全失败了.

My problem is now how I can access IsChecked and based on the current value of it change the background of grid? Haven't done that before only tried it some time ago and totally failed.

先谢谢您了:)

这也是UserControl:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="IFCSMainInterface.MainMenuButton"
x:Name="UserControl"                            
d:DesignWidth="640" d:DesignHeight="480" Width="272" Height="110" Template="{DynamicResource MainMenuButtonStyle}">

<Grid x:Name="LayoutRoot"/>

推荐答案

我认为问题出在这里:

<ControlTemplate x:Key="MainMenuButtonStyle" TargetType="{x:Type UserControl}">

您必须设置正确的类型才能使用其依赖项属性. 在您的ResourceDictionary中编写类的名称空间,然后将类型正确地放入控件模板中,例如:

You must set the correct type in order to use its dependency property. Write in your ResourceDictionary the namespace of the class, and put your type correctly in control template, eg:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:my="clr-namespace:ProjectName.NameSpace">
    <ControlTemplate TargetType="my:MainMenuButton" x:Key="MainMenuButtonStyle"> 
          <!-- ... -->
    </ControlTemplate>
 </ResourceDictionary>


编辑(现在已说明):

您正在尝试在xaml自己的控件中定义模板 这样做似乎不太正确,我建议您寻找其他方法,例如创建使用Generic.xaml

You're trying to define a template in xaml own control That does not seem quite right to do, I suggest looking for other approaches, such as creating a control that uses a Generic.xaml

( http ://utahdnug.org/blogs/xamlcoder/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx )

但是,如果要使用依赖项属性(执行方式),则可以尝试以下代码(例如):

but if you want to use the dependency properties (the way you're doing) you can try the following code (for example):

<UserControl x:Class="SamplesStack.Controls.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:my="clr-namespace:SamplesStack.Controls">
<UserControl.Template>
    <ControlTemplate TargetType="UserControl">
        <Grid x:Name="LayoutRoot"> 
            <ContentPresenter />
        </Grid>
        <ControlTemplate.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}" Value="True">
                <Setter TargetName="LayoutRoot" Property="Background" Value="Red"/>
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</UserControl.Template>

这将使您的控件按预期工作.虽然不觉得很酷,但使用DataTrigger.

This will make your control work as expected. Though not think very cool use DataTrigger.

这篇关于通过XAML访问`DependencyProperty`并进行评估?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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