gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:57:50 24 4
gpt4 key购买 nike

我已经建立了一个窗口,它的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com