当文本块宽度是样式的一部分时,如何绑定它



我为AutoComplete创建了自己的类,在它的样式中,我将其隐藏起来,如果文本为空,它将显示一个文本块。除了文本为空时,TextBlock小于AutoComplete元素外,其他一切都很好。我使用了Stretched="Fill",但这会使文本块被拉伸,文本块内部的文本也会被拉伸,看起来很难看。我正在考虑将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}"/>

相关内容

  • 没有找到相关文章