2017-09-03 120 views
0

我有以下的ControlTemplate,如下圖所示,我想避免硬編碼的圖像的HIGHT和寬度,而不是我想圖像控件的高度和寬度結合模板內綁定

<Image Source="/Rotate.Pictures;component/Images/error.png" 
     Stretch="Uniform" 
     HorizontalAlignment="Stretch" 
     VerticalAlignment="Stretch" 
     Height="14" 
     Width="14"/> 

給TextBlock的Text(TextBlock named「ErrorText」)屬性path =「Height」。圖像的高度和寬度都應該綁定到TextBlock的文本高度屬性的高度值

有誰知道我該如何實現這一目標?

模板:

<Grid IsSharedSizeScope="True"> 
    <Grid.Resources> 
     <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <StackPanel> 
          <AdornedElementPlaceholder> 
           <Border BorderBrush="Red" BorderThickness="2"/> 
          </AdornedElementPlaceholder> 
          <ItemsControl ItemsSource="{Binding}"> 
           <ItemsControl.ItemTemplate> 
            <DataTemplate> 
             <StackPanel Orientation="Horizontal"> 
              <Image Source="/Rotate.Pictures;component/Images/error.png" Stretch="Uniform" HorizontalAlignment="Stretch" 
                VerticalAlignment="Stretch" 
                Height="14" 
                Width="14"/> 
              <TextBlock x:Name="ErrorText" Text="{Binding ErrorContent}" Foreground="Red"/> 
             </StackPanel> 
            </DataTemplate> 
           </ItemsControl.ItemTemplate> 
          </ItemsControl> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </Grid.Resources> 
+0

之前指定Grid.Column 1我猜你真正想要的是在ErrorContent一個DataTrigger那臺圖像可見性在ErrorContent不爲空時可見。 – Clemens

回答

0

關鍵的答案是引用過去(對照的是,執行線程在 「{綁定的ElementName = ERRORTEXT,路徑=的ActualHeight}」 已經遇到過)。因此,該解決方案採用一個網格,然後指定Grid.Column 0引用的ElementName在Grid.Column 1

 <Style TargetType="{x:Type TextBox}"> 
      <Setter Property="Validation.ErrorTemplate"> 
       <Setter.Value> 
        <ControlTemplate> 
         <StackPanel> 
          <AdornedElementPlaceholder> 
           <Border BorderBrush="Red" BorderThickness="2"/> 
          </AdornedElementPlaceholder> 
          <ItemsControl ItemsSource="{Binding}"> 
           <ItemsControl.ItemTemplate> 
            <DataTemplate> 
             <Grid> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="Auto" /> 
               <ColumnDefinition Width="*" /> 
              </Grid.ColumnDefinitions> 
              <TextBlock x:Name="ErrorText" Grid.Column="1" Text="{Binding ErrorContent}" Foreground="Red"/> 
              <Image Grid.Column="0" Source="/Rotate.Pictures;component/Images/error.png" 
                Height="{Binding ElementName=ErrorText, Path=ActualHeight}" 
                Width="{Binding ElementName=ErrorText, Path=ActualHeight}"/> 
             </Grid> 
            </DataTemplate> 
           </ItemsControl.ItemTemplate> 
          </ItemsControl> 
         </StackPanel> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style>