Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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
C# Windows Phone 10 Xaml ListView.ItemTemplate绑定usercontrol对象属性_C#_Windows 10 Mobile - Fatal编程技术网

C# Windows Phone 10 Xaml ListView.ItemTemplate绑定usercontrol对象属性

C# Windows Phone 10 Xaml ListView.ItemTemplate绑定usercontrol对象属性,c#,windows-10-mobile,C#,Windows 10 Mobile,我正在尝试使用ListView.ItemTemplate中的自定义控件。此自定义控件具有对象属性。我无法绑定到此对象属性。我尝试了以下方法,但出现了错误 我的要求是,MyDataRowProperty需要绑定到分配给listView.ItemsSource的列表中的每个MyDataRow <ListView x:Name="listView" ItemsSource="{Binding}"> <ListView.ItemTemplate>

我正在尝试使用ListView.ItemTemplate中的自定义控件。此自定义控件具有对象属性。我无法绑定到此对象属性。我尝试了以下方法,但出现了错误

我的要求是,MyDataRowProperty需要绑定到分配给listView.ItemsSource的列表中的每个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>
MyDataRow.cs

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

有一些问题需要解决。首先,应该绑定ViewModel中的项,而不是代码隐藏中的项。在ViewModel中,实现INotifyPropertyChanged接口。应该是这样的:

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));
        }
    }
   ....
}
之后,在视图的代码隐藏中设置datacontext,然后可以在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>

希望这有帮助。请随便问。我没有运行这段代码,所以请检查语法,但想法就在那里。 克里斯托夫

public class MyDataRow
{
    public string Description { get; set; }
}
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));
        }
    }
   ....
}
  public string MyDataRow
        {
            get { return (string)GetValue(MyDataRowProperty); }
            set { SetValue(MyDataRowProperty, value); }
        }

    public static readonly DependencyProperty MyDataRowProperty = DependencyProperty.Register("MyDataRow", typeof(string), typeof(MyControl));        
<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>