简体   繁体   中英

Access `DependencyProperty` via XAML and evaluate it?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM