WPF数据绑定未更新

时间:2016-04-27 17:57:34

标签: c# wpf mvvm data-binding

我已经构建了一个具有xaml的窗口,如下所示:

<Window.Resources>
    <DataTemplate x:Key="DataTemplate_Level2">
        <Label Content="{Binding }" Width="70" Height="70" HorizontalContentAlignment="Center" x:Name="Background">
        </Label>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding}" Value="1">
                <Setter TargetName="Background" Property="Background" Value="Black"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding}" Value="5">
                <Setter TargetName="Background" Property="Background" Value="Red"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding }" Value="9">
                <Setter TargetName="Background" Property="Background" Value="Green"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=lst, Mode=OneWay}" Value="7">
                <Setter TargetName="Background" Property="Background" Value="blue"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate_Level1">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</Window.Resources>

<DockPanel>
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="1*"  />
            <RowDefinition Height="30" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20" />
            <ColumnDefinition Width="1*"  />
            <ColumnDefinition Width="20" />
        </Grid.ColumnDefinitions>
        <Grid KeyDown="OnKeyDownHandler" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center">
            <ItemsControl Grid.Row="1" Grid.Column="1"  x:Name="lst" ItemTemplate="{DynamicResource DataTemplate_Level1}"/>
        </Grid>

        <WrapPanel Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">
            <Button Content="Start" Click="Start_Click" FontSize="15" MinHeight="30" MinWidth="90"/>
            <Button Content="Suggestion" Click="Suggestion_Click"  FontSize="15" MinHeight="30" MinWidth="90"/>
            <Button Content="Exit" Click="Back_Click"  FontSize="15" MinHeight="30" MinWidth="90"/>
        </WrapPanel>
    </Grid>
</DockPanel>

现在我收到了&#34; lst&#34;中的所有信息。来自存储在我的模型中的2D列表。在运行期间,当我更改List中的值时,更改根本不会出现在Grid中。

我如何确保这种情况发生?

这是.cs

中的绑定行
 viewModel.Command(gen);
 InitializeComponent();
 lst.ItemsSource = viewModel.VM_Maze;

public List<List<int>> VM_Maze
    {
        get { return model.Maze; }
    }

我不知道为什么列表中的更改没有反映在Gui中。 有人可以帮忙吗?

由于

2 个答案:

答案 0 :(得分:3)

首先,

VM_Maze必须是ObservableCollection<ObservableCollection<int>>类型。

其次,你没有绑定任何内容:

lst.ItemsSource = viewModel.VM_Maze;

你只是分配它。绑定,如在XAML中的{Binding PropertyName},涉及创建一个Binding class实例,它为您提供了许多有用的东西。

此代码将创建一个实际绑定,以及正确处理通知等等:

Binding binding = new Binding { Source = viewmodel, Path = new PropertyPath("VM_Maze") };
BindingOperations.SetBinding(lst, ItemsControl.ItemsSourceProperty, binding);

如您所见,在XAML中绑定更方便。您的ItemsSource="{Binding}"没有做任何事情。 如果此对象viewmodel是该视图的实际DataContext,则ItemsSource="{Binding VM_Maze}"应该有效。

答案 1 :(得分:0)

List不会从INotifyCollectionChanged继承,因此在添加或删除项目时它不会提供通知,为什么不使用ObservableCollection代替 here