0

我有以下设计模式工作的代码,但是一旦我用真正的相等数据运行它,窗口就只显示默认类名称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();
4

1 回答 1

2

你正在更换 Window.Content正在您GridMyLayoutRootViewModel

当 WPF 去渲染视图模型对象时,它DataTemplate在它的.Resources任何内容,因此它使用所有非 UI 对象的默认模板来渲染它,该模板TextBlockText属性设置为ToString()对象的方法.

您可能希望在您的后面设置数据层Grid而不是替换它,因此在构造函数中设置DataContext属性而不是属性Content

public ItemSelectorView(ItemSelectorViewModel itemSelectorViewModel)
{
    InitializeComponent();

    if (!Designer.IsInDesignMode())
    {
        DataContext = itemSelectorViewModel;
    }
}
于 2013-05-06T15:24:49.910 回答