Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/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
WinRT XAML数据绑定_Xaml_C# 4.0_User Controls_Winrt Xaml - Fatal编程技术网

WinRT XAML数据绑定

WinRT XAML数据绑定,xaml,c#-4.0,user-controls,winrt-xaml,Xaml,C# 4.0,User Controls,Winrt Xaml,我有以下情况: public class HubModel { public string Name { get; set; } } 我在ViewModel中创建了一个ObservableCollection,并将HubPage上的DataContext设置为该ViewModel 在我的Hubbage页面上,我有一个名为TestUserControl的简单用户控件 用户控件中的XAML: <UserControl x:Name="userControl" ....

我有以下情况:

public class HubModel
{
    public string Name { get; set; }
}
我在ViewModel中创建了一个ObservableCollection,并将HubPage上的DataContext设置为该ViewModel

在我的Hubbage页面上,我有一个名为TestUserControl的简单用户控件

用户控件中的XAML:

<UserControl
    x:Name="userControl"
    ....>
    <Grid>
        <StackPanel Orientation="Vertical">
            <ItemsControl x:Name="ItemsContainer" ItemsSource="{Binding Collection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Button Margin="0,0,0,20">
                            <StackPanel>
                                <TextBlock Foreground="Black" HorizontalAlignment="Left" FontFamily="Arial" FontSize="42" VerticalAlignment="Center" Name="CurrencyTextBlock" Text="{Binding Path=Text,ElementName=userControl}"></TextBlock>
                            </StackPanel>
                        </Button>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </StackPanel>
    </Grid>
</UserControl>

用户控制代码隐藏:

public ObservableCollection<object> Collection
{
    get { return (ObservableCollection<object>)GetValue(CollectionProperty); }
    set { SetValue(CollectionProperty, value); }
}

public static readonly DependencyProperty CollectionProperty =
    DependencyProperty.Register("Collection", typeof(ObservableCollection<object>), typeof(TestUserControl), new PropertyMetadata(null));


public string Text
{
    get { return (string)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

public static readonly DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(TestUserControl), new PropertyMetadata(string.Empty));
公共可观测集合集合
{
get{return(ObservableCollection)GetValue(CollectionProperty);}
set{SetValue(CollectionProperty,value);}
}
公共静态只读DependencyProperty CollectionProperty=
Register(“Collection”、typeof(observeCollection)、typeof(TestUserControl)、newpropertyMetadata(null));
公共字符串文本
{
获取{return(string)GetValue(TextProperty);}
set{SetValue(TextProperty,value);}
}
公共静态只读DependencyProperty TextProperty=
Register(“Text”、typeof(string)、typeof(TestUserControl)、newpropertyMetadata(string.Empty));
因为我的UserControl不应该知道HubModel,所以我想通过DependencyProperty绑定TextBlock文本路径

HubPage中的XAML:

...
<userControls:TestUserControl Collection="{Binding TestCollection}" Text="Name"/>
...
。。。
...
Collection=“{Binding TestCollection}”将列表设置为my UserControl中的DependencyProperty

Text=“Name”设置属性名称。计划是我的UserControl在DependencyProperty中查找TextBlock文本“Name”,并从绑定类HubModel的属性“Name”中获取值

问题是我的UserControl在DependencyProperty中找到“Name”,并为集合中的每个条目显示“Name”,而不是类中的属性值

这样的事情可能吗?或者在UserControls中绑定的最佳方式是什么。在我的选项中,不应该从绑定的类中知道属性名称

谢谢
Daniel

这里比较棘手的部分是,您实际上是在尝试绑定
绑定
本身的属性(即
绑定.Path
)。这是不可能的,因为
Binding
不是DependencyObject,而
Binding.Path
不是dependency属性。所以你必须后退一步,找到另一种方法

一种方法是创建
TextBlock
的子类,并添加依赖属性
SourceObject
(对于对象,在本例中为“HubModel”),以及
PropertyName
(对于要显示的属性,为“Name”)或类似属性。这将允许您使用反射更新
文本

因此,您将编写此代码而不是
TextBlock

<my:ExtendedTextBlock SourceObject="{Binding}" PropertyName="{Binding Path=Text,ElementName=userControl}" />
(在WPF中,您可以使用多重绑定来实现这一点,但据我所知,WinRT中并不存在这种情况。)

public class ExtendedTextBlock : TextBlock
{
    public object SourceObject
    {
        get { return GetValue(SourceObjectProperty); }
        set { SetValue(SourceObjectProperty, value); }
    }
    public static readonly DependencyProperty SourceObjectProperty = 
        DependencyProperty.Register("SourceObject", 
            typeof(object), 
            typeof(ExtendedTextBlock), 
            new PropertyMetadata(UpdateText)
        );

    public string PropertyName
    {
        get { return GetValue(PropertyNameProperty); }
        set { SetValue(PropertyNameProperty, value); }
    }
    public static readonly DependencyProperty PropertyNameProperty = 
        DependencyProperty.Register("PropertyName", 
            typeof(string), 
            typeof(ExtendedTextBlock), 
            new PropertyMetadata(UpdateText)
        );

    public static void UpdateText(object sender, DependencyPropertyChangedEventArgs args) 
    {
        var owner = (ExtendedTextBlock)sender;
        if (owner.SourceObject == null || string.IsNullOrEmpty(owner.PropertyName))
            return;

        var prop = SourceObject.GetType().GetProperty(PropertyName); 
        if (prop == null)
            return;

        var val = prop.GetValue(SourceObject, null);
        owner.Text = (val == null ? "" : val.ToString());
    }
}