Access `DependencyProperty` via XAML and evaluate it?

▼魔方 西西 提交于 2019-12-25 04:18:10

问题


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.

Here is my 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); }
    }

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>

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.

Thanks in advance :)

Edit: Here is the UserControl aswell:

<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"/>


回答1:


I think the problem is here:

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

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>

Edit (now that was explained):

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>

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



来源:https://stackoverflow.com/questions/12545918/access-dependencyproperty-via-xaml-and-evaluate-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!