2017-06-24 45 views
1

我有我的網頁上這個簡單的的TextBlockUWP - 應用自定義轉換器,通過X設定值:指令的Uid

<TextBlock x:Uid="SettingsPage_StreamQualityTextBlock" 
      Style="{StaticResource SectionTitleStyle}"/> 

Text值從資源文件由x:Uid指令集。

的事情是我想通過StaticResource到自定義轉換器適用於TextBlockText價值,但是當我做這樣的

Text="{Binding Converter={StaticResource TextToUpperCase}}" 

值未設置並不會應用轉換器。

因此,我的問題是:

這在某種程度上可能在XAML做不落後部分的代碼編程修改?

感謝您提前提供任何幫助!

回答

1

如何簡單附加屬性來監視TextBlockText屬性的更改?

public static class Helper 
{ 
    public static bool GetUseUpperCase(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(UseUpperCaseProperty); 
    } 
    public static void SetUseUpperCase(DependencyObject obj, bool value) 
    { 
     obj.SetValue(UseUpperCaseProperty, value); 
    } 
    public static readonly DependencyProperty UseUpperCaseProperty = 
     DependencyProperty.RegisterAttached("UseUpperCase", typeof(bool), typeof(TextBlock), new PropertyMetadata(false, (sender, args) => 
     { 
      var textBlock = (TextBlock)sender; 
      textBlock.RegisterPropertyChangedCallback(TextBlock.TextProperty, (s, e) => 
      { 
       textBlock.Text = textBlock.Text.ToUpper(); 
      }); 
     })); 
} 

然後你只需將其連接到TextBlock需要大寫。

<TextBlock x:Uid="SettingsPage_StreamQualityTextBlock" 
      Style="{StaticResource SectionTitleStyle}" 
      local:Helper.UseUpperCase="True" /> 
+1

工作就像一個魅力,非常感謝! –

0

什麼是你傳遞給該文本塊中值的名稱,猜數據是在某種型號或類別。確定你想綁定的確切屬性的名稱,包括它在TextBinding聲明,像這樣的

Text="{Binding [propertyName],Converter={StaticResource TextToUpperCase}}" 

與你綁定屬性名盒裝括號替換上述屬性名稱。請記住刪除方括號。

希望這會有所幫助。

+0

這就是問題所在,它不在模型或類中,它只是一個文本綁定到本地化資源文件的標籤,值本身是由'x:Uid'指令設置的。 –