Добро пожаловать в форум, Guest  >>   Войти | Регистрация | Поиск | Правила | В избранное | Подписаться
Все форумы / WPF, Silverlight Новый топик    Ответить
 wpf treeview controlTemplate toggleButton  [new]
Che0
Member

Откуда:
Сообщений: 299
Добрый день.
Определил стили для toggleButton в ресурсах и в упор не могу понять как их применить к treeView.
Вижу только общее свойство Style для TreeView.
Но как применить стили к toggleButton внутри TreeView непонятно.
Спасибо.
12 ноя 14, 18:04    [16836258]     Ответить | Цитировать Сообщить модератору
 Re: wpf treeview controlTemplate toggleButton  [new]
Roman Mejtes
Member

Откуда: г. Пермь
Сообщений: 4036
Che0,

у TreeView есть
ItemContainerStyle свойство, задающее стиль элемента TreeViewItem. Если нужно поменять в нём ToggleButton, то в этом стиле нужно переопределить шаблон TreeViewItem, в котором создать кнопку с нужным стилем. и незабыть ItemsPresenter

    <Style TargetType="{x:Type TreeView}" x:Key="TreeViewStyle">
        
        <Setter Property="ItemContainerStyle">
            <Setter.Value>
                <Style TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="helper:TreeViewExtensions.IsChecked" Value="{Binding IsChecked, Mode=TwoWay}" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TreeViewItem}">
                                <ControlTemplate.Resources>
                                    <SolidColorBrush x:Key="GlyphBrush" Color="#444" />
                                    <Style x:Key="ExpandCollapseToggleStyle" TargetType="{x:Type ToggleButton}">
                                        <Setter Property="Focusable" Value="False"/>
                                        <Setter Property="Template">
                                            <Setter.Value>
                                                <ControlTemplate TargetType="{x:Type ToggleButton}">
                                                    <Grid Width="15" Height="13" Background="Transparent">
                                                        <Path Grid.Column="0" x:Name="arrow" Data="M 1,1.5 L 4.5,5 L 8,1.5" HorizontalAlignment="Center" SnapsToDevicePixels="false" Stroke="#666" StrokeThickness="2" VerticalAlignment="Center" RenderTransformOrigin="0.5,0.5">
                                                            <Path.RenderTransform>
                                                                <TransformGroup>
                                                                    <RotateTransform x:Name="arrowtransform" Angle="-90"/>
                                                                </TransformGroup>
                                                            </Path.RenderTransform>
                                                        </Path>
                                                    </Grid>
                                                    <ControlTemplate.Triggers>
                                                        <Trigger Property="IsChecked" Value="True">
                                                            <Trigger.EnterActions>
                                                                <BeginStoryboard>
                                                                    <Storyboard TargetName="arrowtransform" TargetProperty="Angle">
                                                                        <DoubleAnimation To="0" Duration="0:0:0.3"/>
                                                                    </Storyboard>
                                                                </BeginStoryboard>
                                                            </Trigger.EnterActions>
                                                            <Trigger.ExitActions>
                                                                <BeginStoryboard>
                                                                    <Storyboard TargetName="arrowtransform" TargetProperty="Angle">
                                                                        <DoubleAnimation To="-90" Duration="0:0:0.3"/>
                                                                    </Storyboard>
                                                                </BeginStoryboard>
                                                            </Trigger.ExitActions>
                                                        </Trigger>
                                                    </ControlTemplate.Triggers>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                    <Style x:Key="TreeViewItemFocusVisual">
                                        <Setter Property="Control.Template">
                                            <Setter.Value>
                                                <ControlTemplate>
                                                    <Border>
                                                        <Rectangle Margin="0,0,0,0" StrokeThickness="5" Stroke="Black" StrokeDashArray="1 2" Opacity="0"/>
                                                    </Border>
                                                </ControlTemplate>
                                            </Setter.Value>
                                        </Setter>
                                    </Style>
                                    <converters:LeftMarginMultiplierConverter Length="19" x:Key="LengthConverter" />
                                </ControlTemplate.Resources>
                                <StackPanel>
                                    <StackPanel>
                                        <Grid>                                                    
                                            <Border Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Margin="1"
                                                    BorderThickness="{TemplateBinding BorderThickness}"
                                                    Padding="{TemplateBinding Padding}">
                                            
                                                <Grid Margin="{Binding Converter={StaticResource LengthConverter},RelativeSource={RelativeSource TemplatedParent}}">
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="19" />
                                                        <ColumnDefinition />
                                                    </Grid.ColumnDefinitions>
                                                    <ToggleButton x:Name="Expander" Style="{StaticResource ExpandCollapseToggleStyle}" 
                                                                  IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" 
                                                                  ClickMode="Press"
                                                                  Cursor="Arrow"/>

                                                    <ContentPresenter Grid.Column="1" x:Name="PART_Header" ContentSource="Header" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
                                                </Grid>
                                            </Border>
                                            <!--</ToggleButton>-->
                                            <Rectangle x:Name="FocusBorder" StrokeThickness="1" Stroke="Gray" StrokeDashArray="1 2" SnapsToDevicePixels="true" Visibility="Collapsed" Margin="1"/>
                                        </Grid>
                                        <ItemsPresenter x:Name="ItemsHost" />
                                    </StackPanel>
                                </StackPanel>
                                <ControlTemplate.Triggers>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="helper:TreeViewExtensions.HasCheckedItems" Value="True"/>
                                            <Condition Property="helper:TreeViewExtensions.IsChecked" Value="False"/>
                                        </MultiTrigger.Conditions>
                                        <Setter TargetName="Bd" Property="Background">
                                            <Setter.Value>
                                                <DrawingBrush Viewbox="0,0,1,1" Viewport="0,0,7,7" ViewportUnits="Absolute" TileMode="Tile">
                                                    <DrawingBrush.Drawing>
                                                        <DrawingGroup>
                                                            <GeometryDrawing Geometry="M0,0 L0.5,0 L0,.5 z" Brush="#F8EFCF" />
                                                            <GeometryDrawing Geometry="M0,1 L1,0 L1,.5 L.5,1 z" Brush="#F8EFCF" />
                                                        </DrawingGroup>
                                                    </DrawingBrush.Drawing>
                                                </DrawingBrush>
                                            </Setter.Value>
                                        </Setter>
                                    </MultiTrigger>
                                    <Trigger Property="helper:TreeViewExtensions.IsChecked" Value="True">
                                        <Setter TargetName="Bd" Property="Background" Value="#E3BF40"></Setter>
                                    </Trigger>
                                    <Trigger Property="IsExpanded" Value="false">
                                        <Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
                                    </Trigger>
                                    <Trigger Property="HasItems" Value="false">
                                        <Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
                                    </Trigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="HasHeader" Value="false"/>
                                            <Condition Property="Width" Value="Auto"/>
                                        </MultiTrigger.Conditions>
                                        <Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
                                    </MultiTrigger>
                                    <MultiTrigger>
                                        <MultiTrigger.Conditions>
                                            <Condition Property="HasHeader" Value="false"/>
                                            <Condition Property="Height" Value="Auto"/>
                                        </MultiTrigger.Conditions>
                                        <Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
                                    </MultiTrigger>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter TargetName="FocusBorder" Property="Visibility" Value="Visible"/>
                                    </Trigger>
                                    <Trigger Property="IsEnabled" Value="false">
                                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="helper:TreeViewItemMouseOverHelper.IsMouseDirectlyOverItem" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                                                                    To="#F8EFCF" Duration="0:0:0.3"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                                                                    To="Transparent" Duration="0:0:0.3"/>
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </Style.Triggers>

                </Style>
            </Setter.Value>
        </Setter>
    </Style>
12 ноя 14, 19:07    [16836488]     Ответить | Цитировать Сообщить модератору
Все форумы / WPF, Silverlight Ответить