【问题标题】:UWP : Binding an observable collection from a dependency property in a user controlUWP:从用户控件中的依赖属性绑定可观察集合
【发布时间】:2017-09-28 06:34:51
【问题描述】:

我在 UWP 中创建了一个用户控件,并从具有可观察集合类型的依赖项属性中添加了列表视图绑定。我在我的视图中使用了来自我的 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;
}

【问题讨论】:

  • 你如何设置所需的DataContext?
  • @PedroLamas 我设置了

标签: c# xaml binding uwp observablecollection


【解决方案1】:

固定: 我的 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-05
    • 2016-12-19
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多