简体   繁体   English

Windows Phone 10 Xaml ListView.ItemTemplate绑定用户控件对象属性

[英]Windows Phone 10 Xaml ListView.ItemTemplate binding usercontrol object property

I am trying to use a custom control in ListView.ItemTemplate. 我试图在ListView.ItemTemplate中使用自定义控件。 This custom control has an object property. 此自定义控件具有一个对象属性。 I'm not able to bind to this object property. 我无法绑定到该对象属性。 I tried the following but throws error. 我尝试了以下操作,但抛出错误。

My requirement is, MyDataRowProperty needs to be bound to each MyDataRow in the List assigned to listView.ItemsSource. 我的要求是,MyDataRowProperty需要绑定到分配给listView.ItemsSource的List中的每个MyDataRow。

    <ListView x:Name="listView" ItemsSource="{Binding}">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <!--<Controls:DetailItemControl Height="105" Width="400" MyDataRowProperty="{Binding RelativeSource={RelativeSource Self}}"></Controls:DetailItemControl>-->
                    <Controls:DetailItemControl Height="105" Width="400" MyDataRowProperty="{Binding}"></Controls:DetailItemControl>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Code behind: 后面的代码:

List<MyDataRow> rows = new List<MyDataRow>();
rows = GetData();
listView.ItemsSource = rows;

MyControl.xaml MyControl.xaml

<UserControl
    x:Class="MyProject.Controls.MyControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="110"
    d:DesignWidth="400">

    <Grid>
                    <TextBlock x:Name="tblDescription" />
    </Grid>
</UserControl>

MyControl.xaml.cs: MyControl.xaml.cs:

    private MyDataRow _MyDataRow;
    public MyDataRowProperty MyDataRow
    {
        get { return _MyDataRow; }
        set {
            _MyDataRow = value;
            if (_MyDataRow != null)
            {
    tblDescription.Text = _MyDataRow.Description
            }
        }
    }

MyDataRow.cs MyDataRow.cs

public class MyDataRow
{
    public string Description { get; set; }
}

There are some issues to be fixed. 有一些问题需要解决。 First of all you should bind items from your ViewModel, not from code behind. 首先,您应该绑定ViewModel中的项目,而不是后面的代码。 In your ViewModel implement the INotifyPropertyChanged interface. 在您的ViewModel中,实现INotifyPropertyChanged接口。 It should look like this: 它看起来应该像这样:

public class YourViewModel: INotifyPropertyChanged {
    public event PropertyChangedEventHandler PropertyChanged;
    private ObservableCollection <MyDataRow> rows;
    public ObservableCollection<MyDataRow> Rows 
    {
        get {return rows;}
        set {
            Rows = value;
            NotifyPropertyChanged("Rows");
        }
    }
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
   ....
}

In your component you should create a dependency property: 在您的组件中,您应该创建一个依赖项属性:

  public string MyDataRow
        {
            get { return (string)GetValue(MyDataRowProperty); }
            set { SetValue(MyDataRowProperty, value); }
        }

    public static readonly DependencyProperty MyDataRowProperty = DependencyProperty.Register("MyDataRow", typeof(string), typeof(MyControl));        

After that in your View's code behind set the datacontext, then you can Bind it in the xaml. 之后,在View的代码中设置数据上下文,然后可以将其绑定到xaml中。

<ListView x:Name="listView" ItemsSource="{Binding Rows}">
        <ListView.ItemTemplate>
            <DataTemplate>
                    <Controls:DetailItemControl Height="105" Width="400" MyDataRow="{Binding Description}"></Controls:DetailItemControl>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Hope this helps. 希望这可以帮助。 Feel free to ask. 随便问。 I didn't run this code, so check the syntax, but the idea is there. 我没有运行此代码,因此请检查语法,但是想法就在那里。 Kristof 克里斯托夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FindAncestor在ListView.ItemTemplate中不适用于UserControl - FindAncestor does not work for UserControl in ListView.ItemTemplate 在XAML中绑定UserControl的DataContext不起作用(Windows Phone) - Binding the DataContext of a UserControl in XAML is not working (Windows Phone) 无法将属性名称动态传递给 ListView.ItemTemplate 以进行绑定 (UWP) - Unable to pass property name dynamically to ListView.ItemTemplate to bind (UWP) ListView.ItemTemplate的MultiBinding DataTemplate? - MultiBinding DataTemplate for ListView.ItemTemplate? Windows Phone 8.1数据绑定ListView [XAML] - Windows Phone 8.1 Data Binding ListView [XAML] 如何在XAML的ListView.ItemTemplate中引用ListView DataContext? - How can I reference the ListView DataContext from within the ListView.ItemTemplate in XAML? 按特定值中的值对 ListView 或 ListView.ItemTemplate 进行排序 - Sort a ListView or a ListView.ItemTemplate by a value in a specific 将ItemsControl ItemTemplate中的属性绑定到UserControl上的DP不起作用 - Binding a property in an ItemsControl ItemTemplate to a DP on the UserControl is not working 如何将事件添加到 ListView.ItemTemplate - How to add event to ListView.ItemTemplate ItemTemplateSelector 和 ListView.ItemTemplate 的区别 - Difference between ItemTemplateSelector and ListView.ItemTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM