Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 实施定制;路径“;用户控件中的属性_Silverlight_Binding_User Controls_Path - Fatal编程技术网

Silverlight 实施定制;路径“;用户控件中的属性

Silverlight 实施定制;路径“;用户控件中的属性,silverlight,binding,user-controls,path,Silverlight,Binding,User Controls,Path,正如您可能注意到的,ComboBox、ListBox和类似的“选择器”控件使用一些带有后缀“path”的属性,这允许将具有不同属性名称的对象绑定到其datatemplate,而无需更改模板本身 当然,我不知道这种技术的确切名称,但基本上,我想得到的是这样的东西: <my:ACustomControl HeadingPath="SomePropertyOfModelA" ContentPath="OtherPropertyFromModelA" ItemsSource="{Binding M

正如您可能注意到的,ComboBox、ListBox和类似的“选择器”控件使用一些带有后缀“path”的属性,这允许将具有不同属性名称的对象绑定到其datatemplate,而无需更改模板本身

当然,我不知道这种技术的确切名称,但基本上,我想得到的是这样的东西:

<my:ACustomControl HeadingPath="SomePropertyOfModelA" ContentPath="OtherPropertyFromModelA" ItemsSource="{Binding ModelA}"... />
<ItemsControl x:Name="InternalItemsControl" ItemsSource="{Binding Items, Mode=TwoWay}">
3.-在代码隐藏中定义名为Items的依赖项属性

4.-定义一个名为HeadingPath的依赖属性,该属性的类型为string,用于保存业务对象属性名称

5.-创建一个IVaueConverter实现,该实现也继承自DependencyObject,并实现InotifyProperty更改如下

Imports System.Windows.Data
Imports System.ComponentModel

Public Class PathPropertiesValueConverter
    Inherits DependencyObject
    Implements IValueConverter
    Implements INotifyPropertyChanged
    ...
6.-在IValueConverter实现中定义一个名为字符串类型的PathProperty的依赖项属性,以保存属性名称,该属性的值将在转换数据时用作返回值

7.-转换方法时…:

    Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
        If value IsNot Nothing AndAlso Not String.IsNullOrEmpty(PathProperty) Then
            Return value.GetType.GetProperty(PathProperty).GetValue(value, Nothing)
        Else
            Return Nothing
        End If
    End Function
8.-将IValueConverter实现实例化为usercontrol中的静态资源

<local:PathPropertiesValueConverter x:Key="PathConverter" x:Name="HeadingPathConverter" 
    PathProperty="{Binding ElementName=LayoutRoot, Path=DataContext.HeadingPath, Mode=TwoWay}" />

9.-在此之后,您可以将属性名称用作HeadingPath属性中的字符串,以从任何silverlight页面绑定ItemsControl上的ChildItems:

    <StackPanel Orientation="Vertical" >
        <local:MyCustomControl HeadingPath="PropiedadA" Items="{Binding Listado, Mode=TwoWay}" />
        <local:MyCustomControl HeadingPath="PropiedadB" Items="{Binding Listado2, Mode=TwoWay}" />
    </StackPanel>

10.-作为Listado和Listado 2 ObservableCollections,加载了此级别集的数据和所有绑定内容。。。结果是:


我认为您最好的选择是自己创建数据模板。幕后
displaymberpath
基本上是创建一个数据模板,并将一个TextBlock绑定到属性
SelectedValuePath
只是一个DependencyProperty,带有设置所选值绑定路径的PropertyChangedCallback。最终,您创建的任何自定义属性都将成为您在其他地方定义的某些功能的快捷方式。

您好,大卫,谢谢您的快速回答。。。我想你指出的SelectedValuePath正是我想要的。。。但是,在这种情况下,如何更改DataTemplate内部的绑定(比如textblock)?再次感谢@我不知道怎么做,需要一些修修补补才能弄清楚。如果您使用Silverlight 5,您可能可以在数据模板中使用自定义标记扩展来重新定义绑定(例如)。我只有几分钟的时间来尝试一些事情,但遇到了一个障碍,但如果您希望联系,我可以告诉您我离开的位置。您可能会看到,我现在正在使用SL 4,我已编辑问题以添加我的进度。。。正如您所指出的,我使用依赖属性来实现它。由于在datatemplate中绑定而不写入特定的模型属性名称是不可能的,所以我使用了ValueConverters,但我必须做一些变通来处理数据加载的顺序。。。希望你能检查一下,告诉我你的想法。非常感谢你!
<local:PathPropertiesValueConverter x:Key="PathConverter" x:Name="HeadingPathConverter" 
    PathProperty="{Binding ElementName=LayoutRoot, Path=DataContext.HeadingPath, Mode=TwoWay}" />
    <StackPanel Orientation="Vertical" >
        <local:MyCustomControl HeadingPath="PropiedadA" Items="{Binding Listado, Mode=TwoWay}" />
        <local:MyCustomControl HeadingPath="PropiedadB" Items="{Binding Listado2, Mode=TwoWay}" />
    </StackPanel>