2013-02-21 36 views
0

我試圖設置我的自定義組合框時,它有一個驗證錯誤的邊距。 這裏是我嘗試使用代碼:WPF我用於Validation.ErrorTemplate的ControlTemplate無法訪問父控件

<ComboBox x:Class="Emsc.Prestige.Windows.AutoCompleteComboBox" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      x:Name="MyComboBox"> 

    <Validation.ErrorTemplate> 
     <ControlTemplate> 
      <Border x:Name="ErrorBorder" BorderThickness="2" CornerRadius="2"> 
       <AdornerElementPlaceholder x:Name="adorner"/> 
      </Border> 
     <ControlTemplate.Triggers> 
      <DataTrigger Binding="{Binding Path=AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=adorner, Converter={StaticResource ErrorContentToErrorTypeConverter}}" 
                 Value="Critical"> 
       <Setter TargetName="ErrorBorder" Property="BorderBrush" Value="Red" /> 
       <Setter TargetName="MyComboBox" Property="Margin" Value="0,0,10,0" /> 
      </DataTrigger> 
     </ControlTemplate> 
    </Validation.ErrorTemplate> 
</ComboBox> 

所以,當我設置BorderBrush爲ErrorBorder元素,它工作正常。但是,當我嘗試訪問我MyComboBox的Margin屬性(或任何財產),我收到以下錯誤:

The property 'TargetName' does not represent a valid target for the 'Setter' because an element named 'MyComboBox' was not found. Make sure that the target is declared before any Setters, Triggers or Conditions that use it.

我想訪問MyComboBox的保證金在XAML,而不是在後面的代碼。 有沒有辦法通過我的DataTrigger中的Setter訪問「TemplatedParent」?

回答

0

嘗試使用樣式元素,這樣

<Style TargetType="ComboBox"> 
    <Style.Triggers> 
    <Trigger Property="Validation.HasError" Value="True"> 
     <Setter Property="Margin" Value="2" /> 
    </Trigger> 
    </Style.Triggers> 
</Style> 

東西這種風格應用到您的組合框

相關問題