2013-08-20 117 views
0

我爲AutoComplete創建了我自己的類,並且在它的樣式中,我創建了這樣的樣式,如果文本沒有任何內容,則會顯示文本塊。除非TextBlock小於AutoComplete元素,否則一切正常。我使用了Stretched =「Fill」,但這會使TextBlock拉伸,textblock內部的文本也會拉長,看起來非常難看。我正在考慮將textbloxk的寬度綁定到AutoComplete元素的ActualWidth,但問題是textblock處於樣式中,我無法使用ElementName。有什麼方法可以在不使用ElementName的情況下綁定寬度?如何在樣式中綁定TextBlock Width

謝謝你這麼多

<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> 

回答

1

你可以試試這個

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

注意:不能用這個觸發器。

對不起,我沒有注意到它是觸發器的一部分。在這種情況下,你可以使用這些

<TextBlock Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"/> 
<TextBlock Width="{Binding RelativeSource={RelativeSource Self}, Path=Width}"/> 
+0

之一,注意:您要刪除拉伸無以'' – SvenG

+0

喜丹尼爾 - 非常感謝你的回答,但遺憾的是沒有一個能解決問題。 你有其他建議嗎? - 再次感謝你 – Saman

+0

嗨SvenG - 如果我刪除''那麼後面的textblcok將始終被拉伸並且其文本的字體大小也會改變。這使它看起來很醜。就好像如果拉伸只在高度,那麼字體會很長很醜。 - 謝謝 – Saman