[英]WPF Treeview use property value as Binding Path
我正在嘗試使用名為 MachineComponentFault 的自定義類的 ObservableCollection 創建一個 Treeview,該類包含一個名為 FaultText 的字符串屬性,我想讓文本本地化。
我正在使用 Codeproject 中的WPF 運行時本地化在運行時本地化文本,它通常如下工作:
<TextBlock Text="{Binding Path=NameInResources, Source={StaticResource Resources}}"/>
問題是我似乎無法弄清楚如何將屬性的值設置為路徑,以便它可以檢索翻譯。 這是我迄今為止所管理的:
<TreeView Name="myTreeView" VirtualizingPanel.IsVirtualizing="True" ItemsSource="{Binding Faults}">
<TreeView.Resources>
<DataTemplate DataType="{x:Type MassComponents:MachineComponentFault}">
<StackPanel Orientation="Horizontal" >
<TextBlock Name="Text1" Text="{Binding FaultText}"/>
<TextBlock Name="Text2" Text="{Binding Path=FLT_PTC_1, Source={StaticResource Resources}}"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
本質上Text1在Runtime顯示FLT_PTC_1,而Text2顯示“Motor Overheat”,這是Resources.resx中FLT_PTC_1的值(可以翻譯)。 問題是我似乎無法使用 FaultText 屬性執行 Text2 的操作。
有沒有辦法做到這一點?
編輯:
使用 mm8 解決方案解決了它,同時保持了 WPF 運行時本地化。 該解決方案一點也不漂亮,因為它包括在虛擬類上創建一個 Binding,然后將綁定值作為字符串檢索,這似乎有點令人費解,但它是我能找到的最佳解決方案。
public class ResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string resourceName = value as string;
if (!string.IsNullOrEmpty(resourceName)) //look up the resource here:
{
Binding b = new Binding(resourceName); //Create Binding using as Path the value of FaultText
b.Source = CultureResources.ResourceProvider; //Get the resources from WPF Runtime Localization ObjectDataProvider
BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, b); //Set the Binding to the dummy class instance
return _dummy.GetValue(Dummy.ValueProperty); //Retrieve the value of the Binding from the dummy class instance and return it
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
//Initialize Dummy class
private static readonly Dummy _dummy = new Dummy();
//Create a dummy class that accepts the Binding
private class Dummy : DependencyObject
{
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(object), typeof(Dummy), new UIPropertyMetadata(null));
}
}
XAML 與提議的 mm8 相同。
您可以綁定到FaultText
屬性並使用轉換器來查找資源。 像這樣的東西:
public class ResourceConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string resourceName = value as string;
if (!string.IsNullOrEmpty(resourceName)) //look up the resource here:
return Resource1.ResourceManager.GetString(resourceName);
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<TextBlock Name="Text2">
<TextBlock.Text>
<Binding Path="FaultText">
<Binding.Converter>
<local:ResourceConverter />
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.