我有一个带有一些加载集合的按钮的MainWindow:
<Window x:Class="GUI.MainWindow.MainWindow"
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:mainWindowViewModel="clr-namespace:GUI.MainWindow"
xmlns:s="clr-namespace:Helpers"
xmlns:configuration="clr-namespace:GUI.Configuration"
xmlns:results="clr-namespace:GUI.Results"
mc:Ignorable="d"
Title="{Binding Path=Model.Title}"
Width="Auto"
WindowStartupLocation="CenterScreen">
<Window.Resources>
<DataTemplate DataType="{x:Type results:ResultViewModel}">
<results:ResultView/>
</DataTemplate>
</Window.Resources>
<!--DataContext-->
<Window.DataContext>
<mainWindowViewModel:MainWindowViewModel />
</Window.DataContext>
<!--DataContext-->
<!--Main Grid-->
<Grid Name="MainGrid"
Margin="5"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="8*" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!--Tabs-->
<TabControl Grid.Row="1"
Grid.RowSpan="2"
Grid.Column="0"
VerticalAlignment="Stretch">
<!--Result View-->
<TabItem Header="{Binding Model.TabImportHeader}">
<ContentControl Content="{Binding ResultViewModel}"/>
</TabItem>
</TabControl>
<!--Tabs-->
<!--Buttons-->
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Margin="10"
Height="25"
Width="150"
Content="Load"
Command="{Binding LoadCommand}" />
<Button Grid.Row="0"
Grid.Column="2"
Margin="10"
Height="25"
Width="150"
Content="Reload"
Command="{Binding ReloadCommand}" />
</Grid>
<!--Buttons-->
</Grid>
<!--Main Grid-->
</Window>
然后我有一个UserControl,它应显示已加载的集合,但不显示任何内容。
<UserControl x:Class="GUI.Results.ResultView"
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:GUI.Results"
mc:Ignorable="d">
<UserControl.DataContext>
<local:ResultViewModel />
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="87*" />
<ColumnDefinition Width="415*" />
<ColumnDefinition Width="501*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Column="0"
Grid.ColumnSpan="3"
Grid.Row="0"
ItemsSource="{Binding Model.Collections}"
ColumnWidth="*"
AutoGenerateColumns="false"
HorizontalAlignment="Stretch"
IsReadOnly="True">
<DataGrid.Resources>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment"
Value="Center" />
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Header="Filename"
Binding="{Binding ResultInfo.Filename}"
Width="Auto" />
<DataGridTextColumn Header="Score 1"
Binding="{Binding ResultInfo.Score1, StringFormat=F4, ConverterCulture=de-DE}"
Width="Auto" />
<DataGridTextColumn Header="Score 2"
Binding="{Binding ResultInfo.Score2, StringFormat=F4, ConverterCulture=de-DE}"
Width="Auto" />
<DataGridTextColumn Header="Score 3"
Binding="{Binding ResultInfo.Score3, StringFormat=F4, ConverterCulture=de-DE}"
Width="Auto" />
<DataGridTextColumn Header="Score 4"
Binding="{Binding ResultInfo.Score4, StringFormat=F4, ConverterCulture=de-DE}"
Width="Auto" />
<DataGridTextColumn Header="SaMP"
Binding="{Binding ResultInfo.SaMP}"
Width="Auto" />
<DataGridTextColumn Header="Spig"
Binding="{Binding ResultInfo.Spig}"
Width="Auto" />
<DataGridTextColumn Header="Ppig"
Binding="{Binding ResultInfo.Ppig, StringFormat=00.0##\\%}"
Width="Auto" />
<DataGridTextColumn Header="Pigment3"
Binding="{Binding ResultInfo.Pigment3}"
Width="Auto" />
<DataGridTextColumn Header="Pigment4"
Binding="{Binding ResultInfo.Pigment4}"
Width="Auto" />
<DataGridTextColumn Header="ZQ1"
Binding="{Binding ResultInfo.ZQ1}"
Width="Auto" />
<DataGridTextColumn Header="ZQ2"
Binding="{Binding ResultInfo.ZQ2}"
Width="Auto" />
<DataGridTextColumn Header="ZQ3"
Binding="{Binding ResultInfo.ZQ3}"
Width="Auto" />
<DataGridTextColumn Header="ZQ4"
Binding="{Binding ResultInfo.ZQ4}"
Width="Auto" />
<DataGridTextColumn Header="Message"
Binding="{Binding ResultInfo.Message}"
Width="Auto" />
<DataGridTextColumn Header="Distance"
Binding="{Binding ResultInfo.PowDistance, StringFormat=F2, ConverterCulture=de-DE}"
Width="Auto" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
这里我如何将数据放入集合中(如果我调试,值就在那里):
Application.Current.Dispatcher.Invoke(() => MainWindowViewModel.ResultViewModel.Model.Collections.Add(Collection));
Collections
是ObservableCollection的类型&lt;&gt ;;
似乎我在绑定中遗漏了一些东西,但我对WPF和MVVM很新,我找不到它。 我已经阅读了有关DependencyProperty的内容,但我想为我的userControl创建一个ViewModel,因为它最终会变得更加复杂。
我真的很感激任何帮助。
答案 0 :(得分:1)
您有两个ResultViewModel
个实例,一个在MainWindowViewModel.ResultViewModel
属性中,另一个直接分配给UserControl的DataContext:
<UserControl.DataContext>
<local:ResultViewModel />
</UserControl.DataContext>
从UserControl的XAML中删除上述内容。
通常,UserControl永远不应该拥有其“自己的”视图模型,并且您永远不应该在其XAML或代码中显式设置其DataContext属性。这样做会覆盖继承的DataContext的值,该值包含正确的ResultViewModel实例。