• WPF如何获取共通Style中的button单击事件


    <Style x:Key="comboxStyle2" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemContainerStyle">
    <Setter.Value>
    <Style TargetType="ComboBoxItem">
    <Setter Property="Height" Value="30"/>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
    <Grid Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="20" MaxWidth="30" />
    </Grid.ColumnDefinitions>
    <Border x:Name="_borderbg" Background="Transparent"/>
    <TextBlock Margin="3 0 3 0" VerticalAlignment="Center" x:Name="_txt" Foreground="#333" Text="{Binding Content,RelativeSource={RelativeSource TemplatedParent}}"/>
    <Button x:Name="_button" Grid.Column="1" VerticalAlignment="Center" VerticalContentAlignment="Top" HorizontalAlignment="Right" Background="Transparent" BorderThickness="0" Width="20" Height="30" Cursor="Hand" Content="×" Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" CommandParameter="{Binding ElementName=_txt,Path=Text}"/>
    <Border x:Name="_border" Background="White" Opacity="0"/>
    </Grid>
    <ControlTemplate.Triggers>
    <Trigger Property="IsSelected" Value="true">
    <Setter TargetName="_borderbg" Property="Background" Value="{StaticResource ComboBoxSelectdBackground}" />
    <Setter TargetName="_txt" Property="Foreground" Value="{StaticResource ComboBoxSelectedForeground}"/>
    <Setter TargetName="_button" Property="Background" Value="{StaticResource ComboBoxSelectdBackground}"/>
    </Trigger>
    <MultiTrigger>
    <MultiTrigger.Conditions>
    <Condition Property="IsSelected" Value="false"/>
    <Condition Property="IsMouseOver" Value="true"/>
    </MultiTrigger.Conditions>
    <Setter TargetName="_borderbg" Property="Background" Value="{StaticResource ComboBoxMouseOverBackground}" />
    <Setter TargetName="_txt" Property="Foreground" Value="{StaticResource ComboBoxMouseOverForegrond}"/>
    <Setter TargetName="_button" Property="Background" Value="{StaticResource ComboBoxMouseOverBackground}"/>
    </MultiTrigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>
    </Setter.Value>
    </Setter>
    <Setter Property="Template">
    <Setter.Value>
    <ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid>
    <Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.7*"/>
    <ColumnDefinition Width="0.3*" MaxWidth="30"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="{TemplateBinding BorderBrush}" CornerRadius="1,0,0,1"/>
    <ContentPresenter HorizontalAlignment="Left" Margin="3,3,0,3" x:Name="ContentSite" VerticalAlignment="Center" Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" IsHitTestVisible="False"/>

    <!--ToggleButton 已数据绑定到 ComboBox 本身以切换 IsDropDownOpen-->
    <ToggleButton Grid.Column="0" Grid.ColumnSpan="2" Template="{StaticResource ComboBoxToggleButton}" x:Name="ToggleButton" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"/>
    <!--必须将 TextBox 命名为 PART_EditableTextBox,否则 ComboBox 将无法识别它-->
    <TextBox Visibility="Hidden" BorderThickness="0" Margin="2 0 0 0" x:Name="PART_EditableTextBox" VerticalAlignment="Center" Focusable="True" Background="Transparent" IsReadOnly="{TemplateBinding IsReadOnly}"/>

    <!--Popup 可显示 ComboBox 中的项列表。IsOpen 已数据绑定到通过 ComboBoxToggleButton 来切换的 IsDropDownOpen-->
    <Popup IsOpen="{TemplateBinding IsDropDownOpen}" Placement="Bottom" x:Name="Popup" Focusable="False" AllowsTransparency="True" PopupAnimation="Slide">
    <Grid MaxHeight="150" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
    <Border x:Name="DropDownBorder" BorderBrush="#e8e8e8" BorderThickness="1 0 1 1"/>
    <ScrollViewer Margin="1" SnapsToDevicePixels="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
    <!--StackPanel 用于显示子级,方法是将 IsItemsHost 设置为 True-->
    <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" Background="White"/>
    </ScrollViewer>
    </Grid>
    </Popup>
    </Grid>
    <ControlTemplate.Triggers>
    <Trigger Property="IsEditable" Value="true">
    <Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible" />
    </Trigger>
    </ControlTemplate.Triggers>
    </ControlTemplate>
    </Setter.Value>
    </Setter>
    </Style>

    主要是通过RelativeSource 进行 DataContext 查找,进而进行绑定。

    Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}" 

    AncestorType的值取决于父窗体所在是WIndow还是UserControl

  • 相关阅读:
    CSS高级应用参考手册
    通过ClassLoader说明容器热部署实现机制
    自定义类加载器
    zz Ubuntu常用命令大全
    设计模式之Decorator(油漆工)
    zz JNI学习(一)、JNI简介和HelloWorld示例
    类加载器的基本概念
    使用 StAX 解析 XML,第 1 部分: Streaming API for XML (StAX) 简介
    ubuntu apache mod_expires模块
    清空ubuntu 日志
  • 原文地址:https://www.cnblogs.com/zhaiganggang/p/15215702.html
Copyright © 2020-2023  润新知