2016-11-02 76 views
1

我爲ContentControl創建了ControlTemplate,該項顯示邊界爲TextBox的背景可以着色。我創建了一個附加屬性來保存一個屬性,該屬性定義是否顯示背景。我似乎無法得到正確的語法綁定到模板中元素的Visibility屬性的附加屬性。ControlTemplate綁定到附加的依賴項屬性

的附加屬性是:

public static class AttachedPropertyExtensions 
{ 
    public static readonly DependencyProperty 
     BackgroundVisible = DependencyProperty.RegisterAttached(
      "BackgroundVisible", 
      typeof(Visibility), 
      typeof(AttachedPropertyExtensions), 
      new PropertyMetadata(default(Visibility))); 

    public static void SetBackgroundVisible(UIElement element, Visibility value) 
    { 
     element.SetValue(BackgroundVisible, value); 
    } 

    public static Visibility GetBackgroundVisible(UIElement element) 
    { 
     return (Visibility)element.GetValue(BackgroundVisible); 
    } 
} 

ControlTemplate

<ControlTemplate x:Key="BorderedTextBlock" TargetType="ContentControl"> 
    <Grid Margin="{StaticResource TextControlMarginThemeThickness}" 
      BorderBrush="{StaticResource TextBoxBorderThemeBrush}" 
      BorderThickness="{StaticResource TextControlBorderThemeThickness}"> 
     <Border x:Name="backgroundBorder" 
       Background="{TemplateBinding Background}" 
       Visibility="{Binding Path=con:AttachedPropertiesExtensions.BackgroundVisible, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 

     <ScrollViewer HorizontalScrollMode="Disabled" 
         VerticalScrollMode="Enabled" 
         VerticalScrollBarVisibility="Visible"> 
      <ContentPresenter Height="80" 
           TextWrapping="Wrap" 
           Margin="{StaticResource TextControlThemePadding}" /> 
     </ScrollViewer> 

    </Grid> 
</ControlTemplate> 

並且這些是使用與用於:

<UserControl ... 
      xmlns:con="using:Scanners.Tetra.UWPmvvm.Helpers"> 
... 
    <ContentControl x:Name="lblReturnText" 
        Template="{StaticResource BorderedTextBlock}" 
        Content="{Binding ReturnText}" 
        Background="#DDDDDD" 
        con:AttachedPropertyExtensions.BackgroundVisible="{Binding ReturnText, Converter={StaticResource HasContentConverter}}" /> 
    <ContentControl x:Name="lblErrorText" 
        Template="{StaticResource BorderedTextBlock}" 
        Content="{Binding ErrorText}" 
        Background="#C03556" 
        con:AttachedPropertyExtensions.BackgroundVisible="{Binding ErrorText, Converter={StaticResource HasContentConverter}}" /> 
</UserControl> 

HasContentConverter:

class HasContentConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     string val = (string)value; 
     string.IsNullOrWhiteSpace(val)) 
     { 
      return Visibility.Visible; 
     } 
     else 
     { 
      return Visibility.Collapsed 
     } 
    ... 

當應用程序被運行(部署在ARM移動設備上),被顯示在輸出

Error: BindingExpression path error: 'con:AttachedPropertiesExtensions' property not found on 'Windows.UI.Xaml.Controls.ContentControl'. BindingExpression: Path='con:AttachedPropertiesExtensions.BackgroundVisible' DataItem='Windows.UI.Xaml.Controls.ContentControl'; target element is 'Windows.UI.Xaml.Controls.Border' (Name='backgroundBorder'); target property is 'Visibility' (type 'Visibility') 

當我改變跟隨誤差

Path=con:AttachedPropertiesExtensions.BackgroundVisible 

Path=(con:AttachedPropertiesExtensions.BackgroundVisible) 

(或帶括號的任何東西)在建立整個ControlTemplate時發生錯誤:

The text associated with this error code could not be found. 
E_UNKNOWN_ERROR 

如何設置綁定到屬性?

+1

首先,正確命名靜態只讀域:'公共靜態只讀的DependencyProperty BackgroundVisibleProperty = ... 。' –

+0

好的,但附加的屬性似乎對'ContentControl'有效:HasContentConverter中的一個斷點成功中斷,並且輸出中沒有消息。只有當在實際模板 –

回答

0

我不能在這裏測試UWP,因爲我們仍然在Win7上,但在WPF中,您附加的屬性定義沒問題,包括名稱。看起來我對這部分錯了。我仍然會重命名它只是遵循約定,但WPF不在乎。也許UWP會 - 我會確定的。

對我來說破了什麼,測試你的代碼,只是綁定路徑。它需要Path周圍的括號,因爲它是一個附加屬性,而且他們有複雜的不遵循的C#屬性命名約定的名稱:整個字符串表示

Visibility="{Binding (con:AttachedPropertyExtensions.BackgroundVisible), RelativeSource={RelativeSource TemplatedParent}}" 

Path周圍的括號告訴它一個單一的精心製作屬性名稱,所以DependencyProperty定義字段爲con:AttachedPropertyExtensions.BackgroundVisible。否則,它認爲con:AttachedPropertyExtensions應該是模板父代的一個屬性,它具有其自己的名稱爲BackgroundVisible的屬性。但這甚至沒有意義。即使這不是語法錯誤:

public Visibility con:AttachedPropertyExtensions{ get; set; } 

......這不是你要在這裏溝通的東西。

如果我添加PresentationTraceSources.TraceLevel=High你原來Binding,這裏的調試跟蹤輸出的一部分,其中失敗:

System.Windows.Data Warning: 108 : BindingExpression (hash=4917414): At level 0 - for ContentControl.con:AttachedPropertyExtensions found accessor <null>

System.Windows.Data Error: 40 : BindingExpression path error: 'con:AttachedPropertyExtensions' property not found on 'object' ''ContentControl' (Name='')'. BindingExpression:Path=con:AttachedPropertyExtensions.BackgroundVisibility; DataItem='ContentControl' (Name=''); target element is 'Border' (Name=''); target property is 'Visibility' (type 'Visibility')

System.Windows.Data Warning: 103 : BindingExpression (hash=4917414): Replace item at level 1 with {NullDataItem}

+1

的邊界上綁定到'Visibility'時才使用。我曾嘗試過,但Visual Studio在編譯時拋出了一個'E_UNKNOWN_ERROR'錯誤。我剛剛嘗試在午餐時間關閉Visual Studio,並且這次它工作。我的猜測是VS變得困惑(它似乎對UWP有很大的影響)。謝謝 –

+0

@JonathanTwite從2008年左右開始,Visual Studio就更容易與每個版本混淆了。 –