C# 通过XAML访问“DependencyProperty”并对其进行评估?
我刚刚为我的自定义按钮创建了一个C# 通过XAML访问“DependencyProperty”并对其进行评估?,c#,wpf,user-controls,dependency-properties,controltemplate,C#,Wpf,User Controls,Dependency Properties,Controltemplate,我刚刚为我的自定义按钮创建了一个dependencProperty。 该属性被称为IsChecked。 如果IsChecked==truemy按钮应将其背景色更改为其他颜色,并保持此颜色,直到IsChecked==false 这是我的DependencyProperty: public static readonly DependencyProperty IsCheckedProperty = DependencyProperty.Register("IsChecked", typeof(boo
dependencProperty
。
该属性被称为IsChecked
。
如果IsChecked==true
my按钮应将其背景色更改为其他颜色,并保持此颜色,直到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>
我认为问题在于:
<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的控件 () 但是,如果您想使用依赖属性(您正在使用的方式),可以尝试以下代码(例如):
这将使您的控件按预期工作。虽然我不认为使用DataTrigger很酷。好的,我尝试了你的解决方案,但它没有完全起作用。现在,该应用程序声称“ControlTemplate”的TargetType'MainMenuButton'与用作模板的Typ'UserControl'不匹配”(翻译自德语的错误消息,不知道确切的英语消息)将my MainMenuButton.xaml编辑到第一篇文章中。您尝试的方法似乎并不理想。我理解这个问题,有办法解决它。但请相信,您不能为扩展自己控件(xaml)中的usercontrol的控件定义模板。在其他控件中使用控件(MainMenuButton)时,可以使用该模板。还有一种非传统的选择。我会编辑我的答案好的,谢谢你的编辑,我会试试的。我刚刚用Expression Blend创建了这个控件,Blend创建了这个模板,所以我认为它可能是最好的解决方案。在某些情况下,甚至是最好的解决方案。VisualStudio2010SP1有两个默认模板:UserControl库和CustomControl库在您的例子中,我相信CustomControl是最好的解决方案。感谢您提供这些信息。我试过你的新解决方案,效果很好。目前,我不担心糟糕的风格或不酷的解决方案:)必须更多地使用XAML,才能更深入地了解它。非常感谢您的帮助!:)
<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>
<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>