簡體   English   中英

UWP:從用戶控件中的依賴項屬性綁定可觀察的集合

[英]UWP : Binding an observable collection from a dependency property in a user control

我在UWP中創建了一個用戶控件,並通過具有可觀察集合類型的依賴項屬性添加了listview綁定。 我在視圖中使用了綁定ViewModel的值。 但這不起作用。 我在列表視圖中看不到任何實體。 我的用戶控件的XAML:

<ListView ItemsSource="{x:Bind TaskLists}"
         SelectionMode="None" IsMultiSelectCheckBoxEnabled="False"  Name="ListofTasks" >

            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="VerticalAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate >
 <TextBlock x:Name="title" FontWeight="SemiBold" 
FontSize="15" Text="{Binding Subject}"  Margin="7,5,0,0" />
</DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

C#代碼背后:

 public ObservableCollection<TaskItem> TaskLists
    {
        get { return (ObservableCollection<TaskItem>)GetValue(TaskListsProperty); }
        set { SetValue(TaskListsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for TaskLists.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TaskListsProperty =
        DependencyProperty.Register("TaskLists", typeof(ObservableCollection<TaskItem>), typeof(TaskList), new PropertyMetadata(null));

我的XAML視圖中的UserControl用法:

        <Controls:TaskList TaskLists="{Binding TodayList ,Mode=TwoWay}" />

查看模型:

class TimeLineViewModel:INotifyPropertyChanged
{

    public TimeLineViewModel()
    {
        for (int i = 0; i < 10; i++)
        {
            TodayList.Add(new TaskItem() { ID = i, Detail = "Lurem IPsum Very cool app is under dev to be abnormal and very secret " + i, Subject = "This is Title of " + i
                , Imprtance = 1, Isdone = 2, Notify = 1, StartTime = DateTime.Now.AddHours(2), Tags= new ObservableCollection<string>() {"Tagone" , "tagtwo","tagThree","TagFour"}
            });

        }
    }
    ObservableCollection<TaskItem> TodayList = new ObservableCollection<TaskItem>();


    public event PropertyChangedEventHandler PropertyChanged;
}

修正:我的ViewModel錯誤。 視圖模型中的固定代碼:

class TimeLineViewModel:INotifyPropertyChanged
{

    public TimeLineViewModel()
    {
        TodayList = new ObservableCollection<TaskItem>();
        for (int i = 0; i < 10; i++)
        {
            TodayList.Add(new TaskItem() { ID = i, Detail = "Lurem IPsum Very cool app is under dev to be abnormal and very secret " + i, Subject = "This is Title of " + i
                , Imprtance = 1, Isdone = 2, Notify = 1, StartTime = DateTime.Now.AddHours(2), Tags= new ObservableCollection<string>() {"Tagone" , "tagtwo","tagThree","TagFour"}
            });

        }
    }

    public ObservableCollection<TaskItem> TodayList { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM