如何在ControlTemplate ..中引用UserControl以及如何触发PropertyDependancy [英] How to Reference a UserControl in a ControlTemplate .. and Trigger on PropertyDependancy

查看:101
本文介绍了如何在ControlTemplate ..中引用UserControl以及如何触发PropertyDependancy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是我不知道如何在UserControl
内的ControlTemplate中引用PropertyDependacyUserControl
我创建了具有多个DependancyProperties的UserControl.这些属性绑定到数据的各个位.
我想创建一个ControlTemplate,该ControlTemplate会在该DependancyProperties的某些值上触发,例如,按钮上会出现另一个图像.
无论如何,出于某些原因,我需要DependancyProperties

这是我的用户控件中的一个示例..我刚刚展示了一个由属性更改触发的效果的简单示例.

我的问题是我不知道如何在ControlTemplate中引用PropertyDependacy或UserControl

The problem I have is that I don''t know how to reference a PropertyDependacy or UserControl in a ControlTemplate inside a UserControl

I have created a UserControl with several DependancyProperties. These properties are bound to various bits of data.
I want to create a ControlTemplate that will trigger on certain values of there DependancyProperties for example a different image appear on a button.

Anyway, there are reasons why I need DependancyProperties

This is a sample of what could be in my user control .. I have just shown a simple example of an effect triggered by a property change

The problem I have is I don''t know how to reference a PropertyDependacy or UserControl in a ControlTemplate

0UserControl x:Class="ConCar.Controls.WPF.StopStartButton"
1  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
4  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
5  mc:Ignorable="d" Height="60" Width="300">
6  <Grid>
7  <Button VerticalAlignment="Stretch" HorizontalAlignment="Stretch"  >
8  <Button.Template>
9    <ControlTemplate TargetType="{x:Type Button}">
10   <Grid>
12     <Image  Name="Normal" HorizontalAlignment="Left"  Width="40" Height="40" Source="Resources/started01.png"/>
14     <Image Name="Pressed" Source="Resources/RedCircle.png" Visibility="Hidden" HorizontalAlignment="Left" Width="40" Height="40" />
15     <Image Name="Disabled" Source="Resources/RedCircle.png" Visibility="Hidden" HorizontalAlignment="Left" Width="40" Height="40" />
17     <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="6 Lane Sizer" Padding="0,0,10,0" FontSize="18" />
18    </Grid>
19    <ControlTemplate.Triggers>
20      <Trigger Property="IsEnabled" Value="True">
21        <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
22        <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
23      </Trigger>
24      <Trigger Property="IsEnabled" Value="False">
25        <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
26        <Setter TargetName="Disabled" Property="Visibility" Value="Visible"/>
27      </Trigger>
28    </ControlTemplate.Triggers>
29    </ControlTemplate>
30    </Button.Template>
31  </Button>
32  </Grid>
33</UserControl>


我想用自己选择的20和24行的DependancyProperty替换IsEnabled.
第10行不应该是该代码所在的UserCoctrol的名称吗?


I want to replace IsEnabled with a DependancyProperty of my own choosing Line 20 and 24.
Shouldn''t Line 10 be the name of the UserCoctrol that this code is in?

推荐答案

对于示例xaml,我将使用ValueConverter作为图像中的图像Source参数,它应该是这样的:
考虑到您具有一个可将控件状态描述为可枚举的属性:
For the sample xaml, I would go with a ValueConverter for the image in the Source parameter, and it would be something like this:
Considering you have a Property that describe the state of your control as an enumerable:
public enum ControlState
{
    Normal = 0,
    Pressed,
    Disabled
}



然后定义ValueConverter:



Then you define the ValueConverter:

public class ControlStateToImageConverter : IValueConverter
{
    #region IValueConverter Members
    public object Convert(object value, Type targetType, 
                     object parameter, System.Globalization.CultureInfo culture)
    {
        ControlState state = (ControlState)value;
        if (state != null)
        {
            BitmapImage img = null;
            switch (state)
            {
                case ControlState.Normal:
                    img = new BitmapImage(
                       new Uri("pack://application:,,,/Resources/started01.png"));
                    break;
                default:
                    img = new BitmapImage(
                       new Uri("pack://application:,,,/Resources/RedCircle.png"));
                    break;
            }
            return img;
        }
        return null;
    }
    public object ConvertBack(object value, Type targetType,
                     object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}


将转换器添加到用户控件的资源中:


Add the converter to the resources of your user control:

<UserControl.Resources>
    <local:ControlStateToImageConverter x:Key="ImgConverter"/>
</UserControl.Resources>



那么您只需在图像中进行简单的绑定即可:



then you just do a simple binding in your image as:

<Image Name="Normal" HorizontalAlignment="Left"  Width="40" Height="40" 

   Source="{Binding ElementName=self, Path=IsPressed, Converter={StaticResource ImgConverter}}"

   />



只需记住在此示例x:Name="self"
中向您的UserControl添加x:Name
这种方法为您提供了一些优点,例如您不需要渲染3个图像控件(即使它们被隐藏,它们也将被渲染),您可以使用INotifyPropertyChanged跟踪对CLR属性的更改,也可以使用DependencyProperty来自动跟踪以下属性的更改:你.但是,如果您真的想制作一个允许模板化的控件,则应该使用从您选择的基本控件扩展的类开始,实现所需的DependencyProperties,添加所需的处理程序并添加一个静态构造函数,如下所示:



just remember to add a x:Name to your UserControl in this sample x:Name="self"

This approach gives you some advantages like you dont need to render 3 Image Controls (even if they are Hidden, they are rendered) you can track the changes to CLR properties with the INotifyPropertyChanged or use DependencyProperty which will automaticaly track the changes in the property for you. But if you are really into making a control that allow templating you should start by using a class that extends from a base control of your choice, implement the DependencyProperties that you need, add the handlers that you need and add a static constructor like this:

static CustomControl1()
{
    DefaultStyleKeyProperty.OverrideMetadata(
        typeof(CustomControl1), 
        new FrameworkPropertyMetadata(typeof(CustomControl1)));
}



还要记住还要添加您需要使用的TemplateParts.我强烈建议您阅读:
http://xamlcoder.com/cs/blogs/joe/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx [在WPF中创建无外观的自定义控件 [



Remember also to add the TemplateParts that you need to use. I would strongly recomend that you read:
http://xamlcoder.com/cs/blogs/joe/archive/2007/12/13/building-custom-template-able-wpf-controls.aspx[^]
Creating a look-less custom control in WPF[^]

Hope it helps you.

All best


这篇关于如何在ControlTemplate ..中引用UserControl以及如何触发PropertyDependancy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆