none
如何动态修改DataTemplate里面的控件的绑定???请看我下面的例子。 RRS feed

  • 问题

  • ++++++
    <ItemsControl x:Class="ErpApplication.Control.SelectItemControl"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:local="clr-namespace:ErpApplication.Control"
                 xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core"
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <ItemsControl.Resources>
            
            <ControlTemplate x:Key="SelectItemTemplate" TargetType="{x:Type CheckBox}">
                <Grid x:Name="Root"  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="CheckIcon">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked"/>
                            <VisualState x:Name="Indeterminate">
                                <Storyboard>
                                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="IndeterminateState">
                                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                                    </DoubleAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="{TemplateBinding Padding}">
                        <Border x:Name="DefaultState" Background="#f5f5f5" CornerRadius="3"/>
                        <Border x:Name="FocusState" CornerRadius="3"></Border>
                        <Border x:Name="HoverState" CornerRadius="3"></Border>
                        <Border x:Name="PressedState" CornerRadius="3"></Border>
                        <Border x:Name="CheckIcon" Background="DodgerBlue" Opacity="0" CornerRadius="3"></Border>
                        <!--<Grid x:Name="FocusState" Background="Blue"></Grid>-->
                        <ContentPresenter  x:Name="ContentPresenter"  ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Grid.Column="1" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="4,0,0,0" RecognizesAccessKey="True" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Opacity" TargetName="HoverState" Value="1"/>
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Opacity" TargetName="PressedState" Value="1"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="Opacity" TargetName="FocusState" Value="1"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Opacity" TargetName="Root" Value="0.35"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
            
        </ItemsControl.Resources>
        
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel IsItemsHost="True" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <CheckBox x:Name="CheckBox" MinHeight="40" MinWidth="80" 
                          Content="{Binding Name}" 
                          IsChecked="{Binding IsCheck}" 
                          Template="{StaticResource SelectItemTemplate}">
                    
                </CheckBox>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        
    </ItemsControl>
    

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace ErpApplication.Control
    {
        /// <summary>
        /// SelectItemControl.xaml 的交互逻辑
        /// </summary>
        public partial class SelectItemControl : ItemsControl
        {
            public SelectItemControl()
            {
                InitializeComponent();
            }
    
            public static readonly DependencyProperty CheckFieldNameProperty = DependencyProperty.Register(
                "CheckFieldName", typeof (string), typeof (SelectItemControl), new PropertyMetadata(null, CheckFieldNameChanged));
    
            public string CheckFieldName
            {
                get { return (string) GetValue(CheckFieldNameProperty); }
                set { SetValue(CheckFieldNameProperty, value); }
            }
    
            public static readonly DependencyProperty ShowTextFieldNameProperty = DependencyProperty.Register(
                "ShowTextFieldName", typeof (string), typeof (SelectItemControl), new PropertyMetadata(null, ShowTextFieldNameChanged));
    
            public string ShowTextFieldName
            {
                get { return (string) GetValue(ShowTextFieldNameProperty); }
                set { SetValue(ShowTextFieldNameProperty, value); }
            }
    
    
            private static void CheckFieldNameChanged(DependencyObject send, DependencyPropertyChangedEventArgs e)
            {
                SelectItemControl itemControl = (SelectItemControl) send;
            }
            private static void ShowTextFieldNameChanged(DependencyObject send, DependencyPropertyChangedEventArgs e)
            {
    
            }
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                var check = this.GetTemplateChild("CheckBox");
                var t = this.ItemTemplate.Template;
                //var binding = check.BindingGroup;
            }
        }
    }
    

    2016年5月16日 9:28

答案

  • 您好 LixyZwx,

    Binding的Path不是依赖属性,所以我们无法动态的去绑定Binding的Path到另外一个属性上面去。我们可以做的就是动态的创建Template并赋值给控件的ItemTemplate属性。您可以把XAML中的ItemTemplate定义去掉,然后使用以下代码。

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    
        DataTemplate template = new DataTemplate();
        FrameworkElementFactory checkBoxElement = new FrameworkElementFactory(typeof(CheckBox), "checkBox");
        checkBoxElement.SetBinding(CheckBox.ContentProperty, new Binding(ShowTextFieldName));
        checkBoxElement.SetBinding(CheckBox.IsCheckedProperty, new Binding(CheckFieldName));
        checkBoxElement.SetValue(CheckBox.TemplateProperty, this.Resources["SelectItemTemplate"]);
        checkBoxElement.SetValue(CheckBox.TemplateProperty, this.Resources["SelectItemTemplate"]);
        template.VisualTree = checkBoxElement;
        this.ItemTemplate = template;
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月17日 6:34
  • 没必要搞这么复杂,用DataTemplateSelector
    2016年5月22日 16:52

全部回复

  • 您好 LixyZwx,

    Binding的Path不是依赖属性,所以我们无法动态的去绑定Binding的Path到另外一个属性上面去。我们可以做的就是动态的创建Template并赋值给控件的ItemTemplate属性。您可以把XAML中的ItemTemplate定义去掉,然后使用以下代码。

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
    
        DataTemplate template = new DataTemplate();
        FrameworkElementFactory checkBoxElement = new FrameworkElementFactory(typeof(CheckBox), "checkBox");
        checkBoxElement.SetBinding(CheckBox.ContentProperty, new Binding(ShowTextFieldName));
        checkBoxElement.SetBinding(CheckBox.IsCheckedProperty, new Binding(CheckFieldName));
        checkBoxElement.SetValue(CheckBox.TemplateProperty, this.Resources["SelectItemTemplate"]);
        checkBoxElement.SetValue(CheckBox.TemplateProperty, this.Resources["SelectItemTemplate"]);
        template.VisualTree = checkBoxElement;
        this.ItemTemplate = template;
    }
    Best Regards,
    Li Wang

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2016年5月17日 6:34
  • 没必要搞这么复杂,用DataTemplateSelector
    2016年5月22日 16:52