【问题标题】:Binding inside of a binding绑定内部的绑定
【发布时间】:2014-03-06 19:13:56
【问题描述】:

好的,我有一个 Observable 集合,其中包含这样定义的字符串。

public ObservableCollection<string> OCGroundType { get; set; }

这个集合有资源键,所以我想要做的是使用这个代码

            <ListBox HorizontalAlignment="Left" Height="110" Margin="156,23,0,0" VerticalAlignment="Top" Width="314" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=OCGroundType}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label Content="{Binding Path=, Source={StaticResource Resources}}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

所以我想要做的是给路径提供 itemsource 的值是可能的吗?

编辑

这就是静态资源“绑定”到的资源字典

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cultures="clr-namespace:Marcam.Cultures"
    xmlns:properties="clr-namespace:Marcam.Properties">

   <ObjectDataProvider x:Key="Resources" ObjectType="{x:Type cultures:CultureResources}" MethodName="GetResourceInstance"/>
</ResourceDictionary>

ObjectType 被“绑定”到的是一个返回当前 Resources.resx 的方法,如果我已经很好地理解它是如何工作的,因为我已经将此代码基于 WPF Localization

【问题讨论】:

    标签: c# wpf binding listbox


    【解决方案1】:

    不可能,因为Binding不是DependencyObject,所以不能绑定它的属性。

    但是,您可以通过使用从资源中获取适当值的转换器来实现所需的结果。


    编辑:首先,您需要这样的转换器:

    public class ResourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string key = (string)value;
            return Properties.Resources.ResourceManager.GetObject(key, culture);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    然后,在 XAML 资源中声明此转换器的一个实例:

    <local:ResourceConverter x:Key="resourceConverter" />
    

    最后,只需在绑定中使用此转换器:

    <Label Content="{Binding Path=., Converter={StaticResource resourceConverter}}"/>
    

    【讨论】:

    • 嗯,我可以使用不止一根骨头^^''。我是 wpf 的新手,你能指出我更具体的方向吗?因为我从未使用过“转换器”
    • 基本上,当前绑定所做的是从 Observable Collection 中获取一个键(由我定义),并且这个键在资源字典中是相同的,所以我试图从 OC 中获取并用它来绑定值
    • @Mokmeuh,{StaticResource Resources} 在您的代码中指的是什么?名为Resources 的资源的类型是什么?
    • 好的,我现在明白了,但是使用它不会绑定到资源字典?它会绑定到数据上下文对吗?它指向自己,但是如果我添加源它应该可以工作??,让我测试一下
    • 我在我的 resourceDictionary 中定义了这个&lt;cultures:ResourceConverter x:Key="resourceConverter"/&gt;,但它不起作用(是的,我已经创建了这个类)它正在编译和一切,但它没有在列表框中显示数据
    【解决方案2】:

    假设资源只是在 Window 或 UserControl 的资源部分声明的字符串值,您可以使用 IMultiValueConverter 来实现。

    首先您需要传递 Window/UserControl 的 resource key 和第二个 ResourceDictionary,无论您的资源是如何定义的。

    XAML 将如下所示:

    <Label>
      <Label.Content>
        <MultiBinding Converter="{StaticResource ResourceToValueConverter}">
           <Binding/>
           <Binding Path="Resources" RelativeSource="{RelativeSource
                                   Mode=FindAncestor, AncestorType=Window}"/>
        </MultiBinding>
      </Label.Content>
    </Label>
    

    当然你需要在 XAML 中添加ResourceToValueConverter 的实例。

    第二个是你的转换器代码:

    public class ResourceToValueConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, 
                              System.Globalization.CultureInfo culture)
        {
            return ((ResourceDictionary)values[1])[values[0]];
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes,
                                    object parameter, 
                                    System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    更新

    当您编辑问题以包含该资源为 ObjectDataProvider 时,您可以修改转换器代码。

    您的转换器中有ObjectDataProvider,您可以获取并调用它的方法来获取资源文件(.resx)文件。从资源文件中获取字符串值并返回。

    var provider = ((ResourceDictionary)values[1])["Resources"] as ObjectDataProvider;
    var output = provider.Data;
    return // Get string from resource file and return it.
    

    【讨论】:

    • 使用它不会使用 ObjectDataProvider 所以当我改变本地化时它不会更新值或者我错了xD
    • @Mokmeuh - 我已经更新了答案。看看这是否适合你。
    • 我仍然不确定如何通过输出从键中获取值,它是什么类型的对象?
    • 并且提供者返回一个空异常:S
    • 您需要执行((ResourceDictionary)values[1])["Resources"] as ObjectDataProvider; 才能获得ObjectDataProvider。输出将是您的方法 GetResourceInstance 的返回类型。
    猜你喜欢
    • 2018-05-25
    • 1970-01-01
    • 2013-03-24
    • 2019-07-31
    • 2021-02-13
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多