UserControls无法呈现

时间:2017-03-02 06:19:05

标签: c# wpf xaml mvvm user-controls

好的,首先,我对WPF和XAML缺乏经验,所以任何指针或建议都会非常感激。

我有一个我正在制定的计划程序,我需要一些帮助设置。我之前有过工作,但它没有正确组织。我在ViewModel中有UI元素,我将在MainWindow的初始化时添加到StackPanel。一般不是MVVM样式编码。所以我做了一些观点(UserControls)来显示我拥有的东西,而且大部分都破了。

基本上,我有一个Schedule ViewModel,它有一些参数和一个不同的Room ViewModel列表。每个Room ViewModel都有一个RoomSchedule ViewModel,其中包含RoomEvent ViewModel列表。

我试图为需要显示的内容编写控件。我创建了一个Schedule视图,其中有一个Room视图列表框,Room视图使用RoomEvent视图显示房间的事件。 Room视图使用WPF Extended Toolkit的TimelinePanel,其余控件都是基本控件。一般的想法是:模型为ViewModel提供数据,ViewModel将数据按摩到需要显示的内容。所以一个事件应该知道如何展示自己,一个房间应该知道如何展示自己,并且时间表应该知道如何展示自己。

我遇到的问题是:现在我已经将xaml.cs或ViewModel文件中的所有内容放到适当的位置,控件根本无法渲染。我一直在阅读人们有同样问题的其他SO帖子,但似乎没有一个像这样的初学者的东西。我认为我已经关闭了,似乎正在创建所有控件,并且DataContext的设置正确,但没有任何显示。

这基本上就是我到目前为止所拥有的。为了简洁,我留下了一些xaml样板文件:

Schedule.xaml:

<StackPanel>
    <ListBox ItemsSource="{Binding Rooms}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                 <localcontrols:RoomView ScheduleStart="{Binding ElementName=ScheduleControl, Path=DataContext.Start}"
            </DataTemplate>
        <ListBox.ItemTemplate>
    </ListBox>
</StackPanel>

RoomView.xaml:

<extended:TimelinePanel BeginDate="{Binding localcontrols:ScheduleStart}" EndDate="{Binding localcontrols:ScheduleEnd}"
    <ItemsControl ItemsSource="{Binding Path=mRoomSchedule.mScheduledEvents}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <localcontrols:EventView />
            </DataTemplate>
        </ItemsControls.ItemTemplate>
    </ItemsControl>
</extended:TimelinePanel>

EventView.xaml:

<Border BorderThickness="1" BorderBrush="Black" extended:TimelinePanel.Date="{Binding mStartTime}" extended:TimelinePanel.DateEnd="{Binding mEndTime}">
    <TextBlock Background="{Binding mColor}" Text="{Binding mEventID}" />
</Border>

ScheduleStartScheduleEnd是RoomView.xaml.cs中定义的依赖项属性。我的想法是,Schedule将在其构造函数中设置StartEnd属性,RoomView中的ListBox将绑定到这些属性以设置TimelinePanel的BeginDateEndDate

1 个答案:

答案 0 :(得分:0)

也许你的绑定是错误的。当我需要绑定到依赖项属性时,我使用绑定的 ElementName 功能来说出我想要的控件,并为根节点命名,在本例中为 Root 。这是解决问题的一种方法。

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:Weingartner.Controls"
    x:Class="RoomView"
    x:Name="Root">


    <extended:TimelinePanel
       BeginDate="{Binding ElementName=Root, Path=ScheduleStart}" 
       EndDate="{Binding ElementName=Root, Path=ScheduleEnd}"
    >
        <ItemsControl ItemsSource="{Binding Path=mRoomSchedule.mScheduledEvents}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <localcontrols:EventView />
                </DataTemplate>
            </ItemsControls.ItemTemplate>
        </ItemsControl>
    </extended:TimelinePanel>



</UserControl>