Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
ControlTemplate and Binding problem
Message
De
26/12/2009 04:39:18
 
 
À
24/12/2009 19:44:31
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Versions des environnements
Environment:
C# 3.0
Divers
Thread ID:
01440313
Message ID:
01440672
Vues:
27
Hi,
Thank you for this. But - it doesn't work - it displays exactly the behaviour that I am seeing:

Without the ControlTemplate clicking on the ExpanderButton in the TreeViewItem will expand the node in the tree and also fire the accessor on MyDataItem.IsExpanded.

With the ControlTemplate in place then neither happens.....

At least you have a reproducible case - and your test code is far simpler that the real version I'm working on so I'm going to play with that,

Oh and, FWIW, you're using a different ControlTemplate to me (mine is the XP one - yours looks like Vista?)

One other thing I've just noticed: If, with the ControlTemplate in place, you click on the ExpanderButton then the image toggles but the items aren't shown. Conversely, if you use the code behind button the items are shown but the ExpanderButton image doesn't change....


Best,
Viv




>I threw together a test app (and I do mean threw, didn't clean anything up)
>
>It seems to work for me. Or... I don't get what's wrong, probably the latter.<g>
>
>Code below, or DL here: http://www.wordmasters.com/temp/WpfTreeViewTest.zip
>
>
><Window x:Class="WpfTreeViewTest.Window1"
>    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
>    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>        xmlns:s="clr-namespace:System;assembly=mscorlib" 
>        xmlns:local="clr-namespace:WpfTreeViewTest"
>    Title="Window1" Height="300" Width="300">
>  <Window.Resources>
>    
>    <HierarchicalDataTemplate DataType = "{x:Type local:MyDataItem}"
>                              ItemsSource = "{Binding Path=Children}" >
>      <TextBlock Text="{Binding Path=Text}"></TextBlock>  
>    </HierarchicalDataTemplate>
>    
>    <Style TargetType="TreeViewItem" x:Key="StdTreeViewItemStyle">
>      <Setter Property="Template">
>        <Setter.Value>
>          <ControlTemplate TargetType="TreeViewItem">
>            <Grid>
>              <Grid.ColumnDefinitions>
>                <ColumnDefinition Width="Auto" MinWidth="19" />
>                <ColumnDefinition Width="Auto" />
>                <ColumnDefinition Width="*" />
>              </Grid.ColumnDefinitions>
>              <Grid.RowDefinitions>
>                <RowDefinition Height="Auto" />
>                <RowDefinition />
>              </Grid.RowDefinitions>
>              <ToggleButton IsChecked="False" ClickMode="Press" Name="Expander">
>                <ToggleButton.Style>
>                  <Style TargetType="ToggleButton">
>                    <Style.Resources>
>                      <ResourceDictionary />
>                    </Style.Resources>
>                    <Setter Property="UIElement.Focusable">
>                      <Setter.Value>
>                        <s:Boolean>False</s:Boolean>
>                      </Setter.Value>
>                    </Setter>
>                    <Setter Property="FrameworkElement.Width">
>                      <Setter.Value>
>                        <s:Double>16</s:Double>
>                      </Setter.Value>
>                    </Setter>
>                    <Setter Property="FrameworkElement.Height">
>                      <Setter.Value>
>                        <s:Double>16</s:Double>
>                      </Setter.Value>
>                    </Setter>
>                    <Setter Property="Control.Template">
>                      <Setter.Value>
>                        <ControlTemplate TargetType="ToggleButton">
>                          <Border Padding="5,5,5,5" Background="#00FFFFFF" Width="16" Height="16">
>                            <Path Fill="#00FFFFFF" Stroke="#FF989898" Name="ExpandPath">
>                              <Path.Data>
>                                <PathGeometry Figures="M0,0L0,6L6,0z" />
>                              </Path.Data>
>                              <Path.RenderTransform>
>                                <RotateTransform Angle="135" CenterX="3" CenterY="3" />
>                              </Path.RenderTransform>
>                            </Path>
>                          </Border>
>                          <ControlTemplate.Triggers>
>                            <Trigger Property="UIElement.IsMouseOver">
>                              <Setter TargetName="ExpandPath" Property="Shape.Stroke">
>                                <Setter.Value>
>                                  <SolidColorBrush>#FF1BBBFA</SolidColorBrush>
>                                </Setter.Value>
>                              </Setter>
>                              <Setter TargetName="ExpandPath" Property="Shape.Fill">
>                                <Setter.Value>
>                                  <SolidColorBrush>#00FFFFFF</SolidColorBrush>
>                                </Setter.Value>
>                              </Setter>
>                              <Trigger.Value>
>                                <s:Boolean>True</s:Boolean>
>                              </Trigger.Value>
>                            </Trigger>
>                            <Trigger Property="ToggleButton.IsChecked">
>                              <Setter TargetName="ExpandPath" Property="UIElement.RenderTransform">
>                                <Setter.Value>
>                                  <RotateTransform Angle="180" CenterX="3" CenterY="3" />
>                                </Setter.Value>
>                              </Setter>
>                              <Setter TargetName="ExpandPath" Property="Shape.Fill">
>                                <Setter.Value>
>                                  <SolidColorBrush>#FF595959</SolidColorBrush>
>                                </Setter.Value>
>                              </Setter>
>                              <Setter TargetName="ExpandPath" Property="Shape.Stroke">
>                                <Setter.Value>
>                                  <SolidColorBrush>#FF262626</SolidColorBrush>
>                                </Setter.Value>
>                              </Setter>
>                              <Trigger.Value>
>                                <s:Boolean>True</s:Boolean>
>                              </Trigger.Value>
>                            </Trigger>
>                          </ControlTemplate.Triggers>
>                        </ControlTemplate>
>                      </Setter.Value>
>                    </Setter>
>                  </Style>
>                </ToggleButton.Style>
>              </ToggleButton>
>              <Border BorderThickness="{TemplateBinding Border.BorderThickness}" 
>Padding="{TemplateBinding Control.Padding}" 
>BorderBrush="{TemplateBinding Border.BorderBrush}" 
>Background="{TemplateBinding Panel.Background}" 
>Name="Bd" SnapsToDevicePixels="True" Grid.Column="1">
>                <ContentPresenter 
>Content="{TemplateBinding HeaderedContentControl.Header}" 
>ContentTemplate="{TemplateBinding HeaderedContentControl.HeaderTemplate}" 
>ContentStringFormat="{TemplateBinding HeaderedItemsControl.HeaderStringFormat}" 
>ContentSource="Header" 
>Name="PART_Header" 
>HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
>SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
>              </Border>
>              <ItemsPresenter Name="ItemsHost" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />
>            </Grid>
>            <ControlTemplate.Triggers>
>              <Trigger Property="TreeViewItem.IsExpanded">
>                <Setter TargetName="ItemsHost" Property="UIElement.Visibility" Value="{x:Static Visibility.Collapsed}" />
>                <Trigger.Value>
>                  <s:Boolean>False</s:Boolean>
>                </Trigger.Value>
>              </Trigger>
>              <Trigger Property="ItemsControl.HasItems">
>                <Setter TargetName="Expander" Property="UIElement.Visibility" Value="{x:Static Visibility.Hidden}" />
>                <Trigger.Value>
>                  <s:Boolean>False</s:Boolean>
>                </Trigger.Value>
>              </Trigger>
>              <Trigger Property="TreeViewItem.IsSelected">
>                <Setter TargetName="Bd" Property="Panel.Background" 
>Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
>                <Setter Property="TextElement.Foreground">
>                  <Setter.Value>
>                    <DynamicResource ResourceKey="{x:Static SystemColors.HighlightTextBrushKey}" />
>                  </Setter.Value>
>                </Setter>
>                <Trigger.Value>
>                  <s:Boolean>True</s:Boolean>
>                </Trigger.Value>
>              </Trigger>
>              <MultiTrigger>
>                <MultiTrigger.Conditions>
>                  <Condition Property="TreeViewItem.IsSelected">
>                    <Condition.Value>
>                      <s:Boolean>True</s:Boolean>
>                    </Condition.Value>
>                  </Condition>
>                  <Condition Property="Selector.IsSelectionActive">
>                    <Condition.Value>
>                      <s:Boolean>False</s:Boolean>
>                    </Condition.Value>
>                  </Condition>
>                </MultiTrigger.Conditions>
>                <Setter TargetName="Bd" 
>Property="Panel.Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
>                <Setter Property="TextElement.Foreground">
>                  <Setter.Value>
>                    <DynamicResource ResourceKey="{x:Static SystemColors.ControlTextBrushKey}" />
>                  </Setter.Value>
>                </Setter>
>              </MultiTrigger>
>              <Trigger Property="UIElement.IsEnabled">
>                <Setter Property="TextElement.Foreground">
>                  <Setter.Value>
>                    <DynamicResource ResourceKey="{x:Static SystemColors.GrayTextBrushKey}" />
>                  </Setter.Value>
>                </Setter>
>                <Trigger.Value>
>                  <s:Boolean>False</s:Boolean>
>                </Trigger.Value>
>              </Trigger>
>            </ControlTemplate.Triggers>
>          </ControlTemplate>
>        </Setter.Value>
>      </Setter>
>      <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
>      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
>      <Style.Triggers>
>        <Trigger Property="IsSelected" Value="True">
>          <Setter Property="FontWeight" Value="Bold" />
>          <Setter Property="Foreground" Value="Red" />
>        </Trigger>
>      </Style.Triggers>
>    </Style>
>  </Window.Resources>
>    <Grid>
>    <Grid.RowDefinitions>
>      <RowDefinition Height="*"/>
>      <RowDefinition Height="30"/>
>    </Grid.RowDefinitions>
>      <TreeView Name="ctrlTree" Grid.Row="0" ItemContainerStyle="{StaticResource StdTreeViewItemStyle}" />
>    <StackPanel Orientation="Horizontal" Grid.Row="1">
>      <Button Click="Button_Click">Test Expand I1</Button>
>      <Button Click="Button_Click_1">Test Sel I1</Button>
>    </StackPanel>
>  </Grid>
></Window>
>
>
>
>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;
>using System.ComponentModel;
>
>namespace WpfTreeViewTest
>  {
>
>  /// <summary>
>  /// Interaction logic for Window1.xaml
>  /// </summary>
>  public partial class Window1 : Window
>    {
>    public List<MyDataItem> Data { get; set; }
>    
>    public Window1()
>      {
>      Data = new List<MyDataItem>();
>      List<MyDataItem> c = new List<MyDataItem>();
>      c.Add(new MyDataItem("Item 1a", null));
>      c.Add(new MyDataItem("Item 1b", null));
>      Data.Add(new MyDataItem("Item 1", c));
>      Data.Add(new MyDataItem("Item 2", c));
>
>      InitializeComponent();
>
>      ctrlTree.ItemsSource = Data;
>      }
>
>    private void Button_Click(object sender, RoutedEventArgs e)
>      {
>      Data[0].IsExpanded = !Data[0].IsExpanded;
>      }
>
>    private void Button_Click_1(object sender, RoutedEventArgs e)
>      {
>      Data[0].IsSelected = !Data[0].IsSelected;
>      }
>    }
>
>  public class MyDataItem : INotifyPropertyChanged
>    {
>    private bool _IsSelected;
>    public bool IsSelected
>      {
>      get { return _IsSelected; }
>      set
>        {
>        if (value != _IsSelected)
>          {
>          _IsSelected = value;
>          RaisePropertyChanged("IsSelected");
>          }
>        }
>      }
>
>    private bool _IsExpanded;
>    public bool IsExpanded
>      {
>      get { return _IsExpanded; }
>      set
>        {
>        if (value != _IsExpanded)
>          {
>          _IsExpanded = value;
>          RaisePropertyChanged("IsExpanded");
>          }
>        }
>      }
>
>    public string Text { get; set; }
>    public List<MyDataItem> Children { get; set; }
>
>    public MyDataItem(string text, List<MyDataItem> children)
>      {
>      Text = text;
>      Children = children;
>      }
>
>    #region INotifyPropertyChanged Members
>
>    public event PropertyChangedEventHandler PropertyChanged;
>
>    protected void RaisePropertyChanged(string property)
>      {
>      PropertyChangedEventHandler hdlr = PropertyChanged;
>      if (hdlr != null)
>        {
>        hdlr(this, new PropertyChangedEventArgs(property));
>        }
>      }
>    #endregion
>    }
>  }
>
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform