对不起,如果标题令人困惑,但我真的不知道如何缩短我的问题.无论如何,在这里.
我正在使用WPF,实体框架和MVVM.
目前,在我的ViewModel中,我有一个属性
public Model.Document Document {get;set;} //Model.Document is an EF Entity
Run Code Online (Sandbox Code Playgroud)
然后,在XAML中,我绑定到
<TextBox Text={Binding Path=Document.Title}/>
Run Code Online (Sandbox Code Playgroud)
标题当然是模型上的属性.
现在我想到了以下问题:要将模型与View分开,如果我将属性添加到ViewModel就不会更好
public string Title
{
get { return Document.Title; }
set { Document.Title = value; }
}
Run Code Online (Sandbox Code Playgroud)
然后像这样绑定:
<TextBox Text={Binding Path=Title}/>
Run Code Online (Sandbox Code Playgroud)
推荐哪种方式?
我有一个带有ItemsSource属性的UserControl.由于基本UserControl类没有实现ItemsSource,我必须创建自己的依赖属性,如下所示:
#region ItemsSource Dependency Property
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(MonthViewControl),
new PropertyMetadata(OnItemsSourceChanged));
static void OnItemsSourceChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
(obj as MonthViewControl).OnItemsSourceChanged(e);
}
private void OnItemsSourceChanged(DependencyPropertyChangedEventArgs e)
{
RefreshLayout();
}
public IEnumerable ItemsSource
{
get
{
return (base.GetValue(ItemsSourceProperty) as IEnumerable);
}
set
{
base.SetValue(ItemsSourceProperty, value);
}
}
#endregion
Run Code Online (Sandbox Code Playgroud)
现在在我的ViewModel中,我有一个Events属性,它是EventItem项的ICollectionView,如下所示:
private ObservableCollection<Controls.EventCalendar.EventItem> eventItems;
private CollectionViewSource events;
public System.ComponentModel.ICollectionView Events
{
get
{
if (events == null)
{
events = new CollectionViewSource();
events.Source = eventItems;
}
return events.View; …Run Code Online (Sandbox Code Playgroud) 我正在学习WPF和MVVM模式,我正在尝试构建一个类似日历的视图.所以我目前有一个6行7列的网格.第一行应该是标题,因此指定周日,如'星期一,星期二等......'我现在在MonthView.xaml中有以下内容
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
<RowDefinition Height="{Binding RowHeight}"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
<ColumnDefinition Width="{Binding CellWidth}"/>
</Grid.ColumnDefinitions>
<!-- Header Row-->
<ContentPresenter Grid.Row="0" Grid.Column="0">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding DaysOfWeek[0]}"/>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="1" DataContext="{Binding DaysOfWeek[1]}"/>
<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" Grid.Column="2" DataContext="{Binding DaysOfWeek[2]}"/>
<ContentPresenter ContentTemplate="{StaticResource CalendarHeaderCellTemplate}" Grid.Row="0" …Run Code Online (Sandbox Code Playgroud)