c# - WPF数据绑定(bind)不更新

标签 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>

现在我从存储在我的模型中的 2D 列表中获取“lst”中的所有信息。在运行时,当我更改列表中的值时,更改根本不会出现在网格中。

我怎样才能确保发生这种情况?

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

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

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

谢谢

最佳答案

第一的,
VM_Maze需要是 ObservableCollection<ObservableCollection<int>> 类型.

其次,您没有在此行中绑定(bind)任何内容:

lst.ItemsSource = viewModel.VM_Maze;

你只是分配它。绑定(bind),如 {Binding PropertyName}在 XAML 中,涉及创建 Binding class为您做了很多有用的事情的实例。

此代码将创建一个实际的绑定(bind),它将正确处理通知等:
Binding binding = new Binding { Source = viewmodel, Path = new PropertyPath("VM_Maze") };
BindingOperations.SetBinding(lst, ItemsControl.ItemsSourceProperty, binding);

如您所见,在 XAML 中绑定(bind)要方便得多。您的 ItemsSource="{Binding}"不做任何事情。如果这个对象 viewmodel是实际的 DataContext对于该 View ,然后 ItemsSource="{Binding VM_Maze}"应该管用。

关于c# - WPF数据绑定(bind)不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36897431/

相关文章:

c# - "Get a place' s 开放和关闭时间,如果 DateTime.Now 位于标记为开放的“在本地工作,但发布后不在服务器上”

c# - 根据年份在 View 中呈现不同的表格

wpf - WPF中的关注点分离

c# - 当 ItemSource 是一个 Observable 集合时,隐藏 DataGrid 中的列

wpf - 在代码中加载BitmapImage

使用 Unity 进行 MVVM 依赖注入(inject),用于分层 View 模型

c# - 我可以有一个方法的扩展方法吗?

c# - Entity Framework : How does the data context deal with duplicate objects?

wpf - 仅在 WPF 中的 ShowDialog() 之后设置 DialogResult

c# - 使用 WPF、MVVM 和 DP 的用户控制