如何使用XAML將非依賴性Text屬性綁定到Application.Current.Resources?如何將非依賴性Text屬性綁定到Application.Current.Resources?
我想在第三方DLL中使用具有非依賴性Text屬性的控件,並且我想將Application.Current.Resources綁定到該屬性。
它不能使用DynamicResource擴展,因爲它是非依賴項屬性。
我該做什麼?
如何使用XAML將非依賴性Text屬性綁定到Application.Current.Resources?如何將非依賴性Text屬性綁定到Application.Current.Resources?
我想在第三方DLL中使用具有非依賴性Text屬性的控件,並且我想將Application.Current.Resources綁定到該屬性。
它不能使用DynamicResource擴展,因爲它是非依賴項屬性。
我該做什麼?
假設您只想顯示第三方控件的Text屬性中的Resources值,可以將第三方控件的Text屬性包裝在WPF attached property中,並根據該屬性綁定/使用DynamicResource。
public static readonly DependencyProperty TextWrappedProperty =
DependencyProperty.RegisterAttached("TextWrapped",
typeof(string), typeof(ThirdPartyControl),
new PropertyMetadata(false, TextWrappedChanged));
public static void SetTextWrapped(DependencyObject obj, string wrapped)
{
obj.SetValue(TextWrappedProperty, wrapped);
}
public static string GetTextWrapped(DependencyObject obj)
{
return (string)obj.GetValue(TextWrappedProperty);
}
private static void TextWrappedChanged(DependencyObject obj,
DependencyPropertyChangedEventArgs e)
{
// here obj will be the third party control so cast to that type
var thirdParty = obj as ThirdPartyControl;
// and set the value of the non dependency text property
if (thirdParty != null)
thirdParty.Text = e.NewValue;
}
我覺得題目應該叫做「如何不依賴Text屬性綁定到Application.Current.Resources」,這是什麼,你真正需要做的 – 2011-05-03 06:21:43