我有以下设计模式工作的代码,但是一旦我用真正的相等数据运行它,窗口就只显示默认类名称DocumentViewer.ViewModel.ItemSelectorViewModel而没有其他(与设计模式不同)。我根本看不到问题。
视图模型
public class ItemSelectorViewModel : ViewModelBase
{
private ObservableCollection<Document> _pictureSelectors;
public ItemSelectorViewModel()
{
if (Designer.IsInDesignMode())
{
string xml = System.IO.File.ReadAllText(GlobalConstants.DesignTimeDataFile);
IList<Document> list = ObjectSerializer.Deserialize<List<Document>>(xml);
PictureSelector = list.ToObservableCollection();
}
}
public ObservableCollection<Document> PictureSelector
{
get { return _pictureSelectors; }
set
{
if (_pictureSelectors == value)
{
return;
}
_pictureSelectors = value;
RaisePropertyChanged("PictureSelector");
}
}
}
看法
public partial class ItemSelectorView
{
public ItemSelectorView(ItemSelectorViewModel itemSelectorViewModel)
{
InitializeComponent();
if (!Designer.IsInDesignMode())
{
Content = itemSelectorViewModel;
}
}
}
XAML
<Window x:Class="DocumentViewer.View.ItemSelectorView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModel="clr-namespace:DocumentViewer.ViewModel"
Title="ItemSelector"
Width="300"
Height="321"
WindowStyle="SingleBorderWindow"
d:DataContext="{d:DesignInstance Type=viewModel:ItemSelectorViewModel,
IsDesignTimeCreatable=True}"
mc:Ignorable="d">
<Window.Resources />
<Grid x:Name="MyLayoutRoot"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>
<DataGrid x:Name="MyDataGrid"
Width="Auto"
Height="Auto"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
CanUserAddRows="False"
ItemsSource="{Binding Path=PictureSelector}"
SelectedCellsChanged="Item_SelectionChanged"
SelectedValuePath="DocId"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding DocNumber}" Header="Tagesnummer" />
<DataGridTextColumn Binding="{Binding GraphicType}" Header="Grafiktyp" />
<DataGridTextColumn Binding="{Binding DocId}" Header="Doc ID" />
</DataGrid.Columns>
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</DataGrid.ColumnHeaderStyle>
</DataGrid>
</Grid>
我用 RelayCommand 从父母那里打电话给孩子
ItemSelectorViewModel viewModel = new ItemSelectorViewModel();
viewModel.PictureSelector = _dataModel.DokumenteItems;
ItemSelectorView view = new ItemSelectorView(viewModel);
view.Owner = Application.Current.MainWindow;
view.Show();