0

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

TorbenJ
  • 4,462
  • 11
  • 47
  • 84

1 Answers1

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.

J. Lennon
  • 3,311
  • 4
  • 33
  • 64
  • Ok I tried your solution but it doesn't fully work. Now the application claims that "TargetType 'MainMenuButton' for 'ControlTemplate' does not match with Typ 'UserControl' used as template" (Translated error message from german, don't know the exact english message) Edited my MainMenuButton.xaml into first post. – TorbenJ Sep 22 '12 at 18:03
  • The approach you're trying to do does not seem ideal. I understand the problem, there are ways to get around it. But do believe you can not define a template for a control that extends a usercontrol in own control(xaml). You can use the template when you use the control (MainMenuButton) inside other controls. There is an alternative unconventional. I'll edit my answer – J. Lennon Sep 22 '12 at 18:20
  • Ok thanks for your edit will try it. I just created this control with Expression Blend and Blend created this template so I thought it might be the best solution. – TorbenJ Sep 22 '12 at 19:37
  • In some cases, even the best solution. The Visual Studio 2010 SP1, has 2 default templates: UserControl Library and CustomControl Library in your case I believe the CustomControl is the best solution. – J. Lennon Sep 22 '12 at 19:41
  • Thanks for this information. I tried your new solution and it works perfectly. At the moment I don't worry about bad style or uncool solutions :) Definetly have to work more with XAML to get more into it. Thank you very much for your help! :) – TorbenJ Sep 22 '12 at 19:46