Ich habe eine Sammlung von Modellen (d. H. Format-Klassen), die ich auf meiner Ansicht (d. H. MainWindowView.xaml) laden möchte. Diese Modelle könnten mehr als einmal geladen werden.Warum wird diese ObservableCollection von ViewModels in View nicht mithilfe von DataTemplates angezeigt?
Um dies zu tun, begann ich, eine ViewModel
und eine DataTemplate
für jede Modellklasse zu erstellen. Zum Beispiel FormatContainer
Modellklasse hat FormatContainerViewModel
als View-Modell:
namespace MyProject.ViewModels
{
public class FormatContainerViewModel : ViewModelBase
{
public FormatContainerViewModel(FormatContainer wrappedFormat)
{
_wrappedFormat = wrappedFormat; // Wrap model.
SubFormats = GetSubFormatsViewModels(_wrappedFormat.SubFormats); // Get the collection of Formats contained into container and generate their ViewModels.
FormatLabel = _wrappedFormat.Description; // Get wrapped format description.
}
private FormatContainer _wrappedFormat;
private ObservableCollection<ViewModel> _subFormats;
public ObservableCollection<ViewModel> SubFormats
{
get
{
return _subFormats;
}
set
{
Set(() => SubFormats, ref _subFormats, value);
RaisePropertyChanged("SubFormats");
}
}
private string _formatLabel;
public string FormatLabel
{
get
{
return _formatLabel;
}
set
{
Set(() => FormatLabel, ref _formatLabel, value);
RaisePropertyChanged("FormatLabel");
}
}
}
}
Zusätzlich Modell FormatContainer eine Datatemplate als Ansicht hat, die von einem Resource hinzugefügt wird:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject.ViewModels">
<DataTemplate DataType="{x:Type local:FormatContainerViewModel}">
<Border BorderBrush="Black" BorderThickness="1" Padding="20">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<!-- Show container description. -->
<TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding FormatLabel}" VerticalAlignment="Center" Margin="5" />
<!-- Show container sub formats. -->
<!--<StackPanel Grid.Column="1" Grid.Row="0" Orientation="Horizontal" Margin="3,3,3,3">
<ContentControl Content="{Binding SubFormats}" HorizontalAlignment="Center" Margin="1" />
</StackPanel-->>
</Grid>
</Border>
</DataTemplate>
<!-- other DataTemplates ... -->
</ResourceDictionary>
Daher ist diese Ressource-Wörterbuch (dh FormatsView XAML) in App.xaml hinzugefügt:
<Application x:Class="MyProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject"
xmlns:vm="clr-namespace:MyProject.ViewModels"
StartupUri="MainWindowView.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:MyProject.ViewModels" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PanelsView.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Dann werden diese Viewmodel in der Hauptansicht Modell instanziiert (dh MainWind owViewModel) nach einer Serviceklasse (d.h. ViewModelsGeneratorService).
<Window x:Class="MyProject.MainWindowView"
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:local="clr-namespace:MyProject"
xmlns:interactivity="using:Microsoft.Expression.Interactivity"
xmlns:core="using:Microsoft.Expression.Interactions.Core"
mc:Ignorable="d"
Title="MySoftware" Height="350" Width="525"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<!-- ... -->
<ContentControl Grid.Row="3" Content="{Binding ViewModelsList}" />
</Grid>
</Window>
Leider mit Inhalt ViewModelsList ist leer, der Content, während ViewModelLists 53 Elemente enthält:
ViewModelsGeneratorService tempGenerator = new ViewModelsGeneratorService(myFormats); // Use a service to convert current format objects to view models.
ViewModelsList = tempGenerator.ViewModelsList; // Populate view models obesrvable collections.
Schließlich diese Ansicht Modelle sind in MainWindowView von einem Content gezeigt. Was fehlt mir? Würde jemand bitte einen Zeiger zur Verfügung stellen?
Ich habe bereits einige Tutorial und Diskussion, um meinen Code zu überprüfen (zB Rachel, Gala, Marco, etc.), aber ich konnte es nicht herausgefunden, was ich bin fehlt ..
Moveover, die Content zeigt die Unterformate nicht an. Natürlich, wenn unkommentiert ;-) Was vermisse ich auch noch hier?
Alles Gute, Lorenzo
P. S. Ich entwickle derzeit für .NET 4.5 und benutze MVVM Light Framework.
P.P.S. FormatContainer ist das einzige meiner Formate, das Unterformate hat, die anderen Formate ändern nur ihre Eigenschaften und so ihre Ansicht (zB könnte ein Format neben der Beschriftung eine Combobox statt einer Listbox anzeigen und/oder anstelle einer Textbox, etc.).
Ihnen hinzufügen Danke, ich meine MainWindowView aktualisiert von Content mit Item Wechsel: mit <- Sprache: lang-cs -> Die Ansichten sind jedoch noch nicht in der Hauptansicht sichtbar. :( –
@LorenzoR. Irgendwelche Bindungsfehler verfügbar? Was passiert, wenn Sie einen Textblock an die Elementanzahl binden: ' '? Zeigt es die erwartete Anzahl der Elemente? –
grek40
@LorenzoR.Sehen Sie den folgenden Link für eine Möglichkeit, zusätzliche Informationen aus verbindlichen Ausdrücke zu erhalten: https://spin.atomicobject.com/2013/12/11/wpf-data-binding-debug/ – grek40