获取父控件模板WPF的属性

问题描述 投票:0回答:1

我希望 POPUP 内的 ComboBox 项目采用 ToggleButton 的高度和宽度,对于宽度来说很好,因为我可以将它绑定在 POPUP 上,但高度适用于每个 ComboBox 项目,它是一个单独的 ControlTemplate。如何绑定(例如将一个控件模板转到另一个控件模板并获取切换按钮的属性)?

<Style x:Key="CBBaseStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <Grid>
                    <Grid Margin="0 0 0 5" x:Name="GridOne">
                        <ToggleButton IsChecked="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}">
                            <ToggleButton.Template>
                                <ControlTemplate TargetType="{x:Type ToggleButton}">
                                    <Border SnapsToDevicePixels="true" />
                                </ControlTemplate>
                            </ToggleButton.Template>
                        </ToggleButton>
                        <ContentPresenter Content="{TemplateBinding SelectionBoxItem}"
                                          VerticalAlignment="Center"
                                          HorizontalAlignment="Center" />
                    </Grid>
                    <Popup Placement="Bottom" 
                           IsOpen="{TemplateBinding IsDropDownOpen}" 
                           PopupAnimation="Slide"
                           AllowsTransparency="True">
                        <Border Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualWidth}">
                            <Grid>
                                <ItemsPresenter />
                            </Grid>
                        </Border>
                    </Popup>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


<Style x:Key="ConboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                <!-- The height for the combobox item i want to come from the ToggleButton height
                     I'm not sure how to bind outside a control template -->
                <Border Height="30">
                    <ContentPresenter ContentSource="Content" />
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
wpf xaml combobox controltemplate
1个回答
0
投票

只需命名

ToggleButton
并使用
Binding.ElementName
通过该名称绑定到它。

ComboBoxItem
显示在
ItemsPresenter
所在的位置 -
ControlTemplate
ComboBox
内。这意味着它们将共享相同的名称范围。
仅出于此目的,您不必为
Controltemplate
定义新的
ComboBoxItem
。样式设置器就足够了。

应用程序.xaml

<Style TargetType="{x:Type ComboBoxItem}">
  <Setter Property="Height"
          Value="{Binding ElementName=Expander, Path=Height}" /> 
  <Setter Property="Width"
          Value="{Binding ElementName=Expander, Path=Width}" /> 
</Style>

<ControlTemplate x:Key="ComboBoxTemplate" 
                 TargetType="{x:Type ComboBox}">
  <Grid>
    <Grid Margin="0 0 0 5"
          x:Name="GridOne">
      <ToggleButton x:Name="Expander"
                    IsChecked="{TempalteBinding IsDropDownOpen}">

      </ToggleButton>
      <ContentPresenter Content="{TemplateBinding SelectionBoxItem}"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center" />
    </Grid>
    <Popup Placement="Bottom"
            IsOpen="{TemplateBinding IsDropDownOpen}"
            PopupAnimation="Slide"
            AllowsTransparency="True">
      <Border>
        <Grid>
          <ItemsPresenter />
        </Grid>
      </Border>
    </Popup>
  </Grid>
</ControlTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.