【问题标题】:Access `DependencyProperty` via XAML and evaluate it?通过 XAML 访问“DependencyProperty”并对其进行评估?
【发布时间】:2012-09-22 17:05:32
【问题描述】:

我刚刚为我的自定义按钮创建了一个DependencyProperty。 该属性称为IsChecked。 如果IsChecked == true 我的按钮应将其背景颜色更改为其他颜色并保持此颜色直到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 这个按钮:

<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 的背景?以前没做过,前段时间才试过,完全失败了。

提前致谢:)

编辑: 这里也是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"/>

【问题讨论】:

    标签: c# wpf user-controls dependency-properties controltemplate


    【解决方案1】:

    我认为问题出在这里:

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

    您必须设置正确的类型才能使用它的依赖属性。 在 ResourceDictionary 中写入类的命名空间,并将类型正确放入控件模板中,例如:

     <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 的控件

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

    但是如果你想使用依赖属性(你正在做的方式)你可以试试下面的代码(例如):

    <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 觉得不是很酷。

    【讨论】:

    • 好的,我尝试了您的解决方案,但不能完全正常工作。现在应用程序声称“ControlTemplate 的 TargetType 'MainMenuButton' 与用作模板的 Typ 'UserControl' 不匹配”(从德语翻译的错误消息,不知道确切的英文消息)将我的 MainMenuButton.xaml 编辑到第一篇文章中.
    • 您尝试采用的方法似乎并不理想。我理解这个问题,有办法解决它。但是请相信您不能为在自己的控件(xaml)中扩展用户控件的控件定义模板。当您在其他控件中使用控件 (MainMenuButton) 时,您可以使用该模板。有一个非常规的替代方案。我会编辑我的答案
    • 好的,感谢您的编辑将尝试一下。我刚刚使用 Expression Blend 创建了这个控件,Blend 创建了这个模板,所以我认为它可能是最好的解决方案。
    • 在某些情况下,即使是最好的解决方案。 Visual Studio 2010 SP1 有 2 个默认模板:在您的情况下,UserControl 库和 CustomControl 库我相信 CustomControl 是最好的解决方案。
    • 感谢您提供此信息。我尝试了您的新解决方案,并且效果很好。目前,我不担心糟糕的风格或不酷的解决方案 :) 肯定必须更多地使用 XAML 才能深入了解它。非常感谢您的帮助! :)
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2020-06-11
    • 1970-01-01
    • 2017-01-10
    • 2018-07-18
    • 1970-01-01
    • 2010-11-28
    • 1970-01-01
    相关资源
    最近更新 更多