当它是样式的一部分时如何绑定TextBlock Width

我为自动完成创建了自己的类,在它的风格中,我创建了类,如果文本没有任何内容,它会显示文本块。 除非TextBlock小于AutoComplete元素,否则一切正常。 我使用了Stretched =“Fill”,但这会使TextBlock拉伸,textblock内部的文本也会拉长,看起来非常难看。 我正在考虑将textbloxk的宽度绑定到AutoComplete元素的ActualWidth,但问题是textblock处于样式中,我无法使用ElementName。 有什么方法可以在不使用ElementName的情况下绑定宽度?

非常感谢

<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
xmlns:Microsoft_Windows_Themes2="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300" Background="Aqua">
<Window.Resources>
    <Style x:Key="ComboBoxEditableTextBox" TargetType="{x:Type TextBox}">
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="MinWidth" Value="0"/>
        <Setter Property="MinHeight" Value="0"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <ScrollViewer x:Name="PART_ContentHost" Background="Transparent" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <ControlTemplate x:Key="AutoCompleteComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
        <Grid SnapsToDevicePixels="true">
            <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="1">
                <Grid Grid.IsSharedSizeScope="true">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition SharedSizeGroup="ComboBoxButton" Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBox x:Name="PART_EditableTextBox" Margin="{TemplateBinding Padding}" Style="{StaticResource ComboBoxEditableTextBox}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Grid.Column="1" IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}"/>
                </Grid>
            </Border>
            <Popup x:Name="PART_Popup" AllowsTransparency="true" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Focusable="false">
                <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" Color="Transparent">
                    <Border x:Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1">
                        <ScrollViewer>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Continue"/>
                        </ScrollViewer>
                    </Border>
                </Microsoft_Windows_Themes:SystemDropShadowChrome>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="HasItems" Value="false">
                <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsGrouping" Value="true">
                <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
            </Trigger>
            <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true">
                <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/>
                <Setter Property="Color" TargetName="Shdw" Value="#71000000"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style BasedOn="{StaticResource {x:Type ComboBox}}" TargetType="{x:Type ComboBox}">
        <Setter Property="IsEditable" Value="true"></Setter>
        <Style.Triggers>
            <Trigger Property="IsEditable" Value="True">
                <Setter Property="IsTabStop" Value="false"/>
                <Setter Property="Padding" Value="0,1"/>
                <Setter Property="Template" Value="{StaticResource AutoCompleteComboBoxEditableTemplate}"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="Text" Value="" />
                </MultiTrigger.Conditions>
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush  Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Text="Press F12" Margin="5,5,5,5" VerticalAlignment="Center" HorizontalAlignment="Left" 
                                       Foreground="Gray" Background="White" FontSize="16" Width="{Binding RelativeSource={RelativeSource Self}, Path=Width}"/>
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </MultiTrigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="SelectedItem" Value="{x:Null}"/>
                    <Condition Property="IsEnabled" Value="True"/>
                </MultiTrigger.Conditions>
                <MultiTrigger.Setters>
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="1"/>
                </MultiTrigger.Setters>
            </MultiTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid>
    <ComboBox IsEditable="True" x:Name="TestCB" Width="100" Height="50" />
</Grid>


你可以试试这个

<TextBlock Width="{TemplateBinding Width}"/>

注意:您不能将此用于触发器。

对不起,我没有注意到它是触发器的一部分。 在这种情况下,您可以使用其中之一

<TextBlock Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"/>
<TextBlock Width="{Binding RelativeSource={RelativeSource Self}, Path=Width}"/>
链接地址: http://www.djcxy.com/p/7763.html

上一篇: How to Bind TextBlock Width when it is part of style

下一篇: binding to the ToString() method in a DataTemplate