【发布时间】:2023-03-28 11:05:01
【问题描述】:
我一直在努力解决如何在页面和 UserControl 之间传递数据,并且可以使用一些说明来说明我可能做错了什么。如果我删除代码的数据部分并仅用 TextBlocks 替换它,我知道 UserControl 正在工作。
我知道 NetWorth 正在显示,但就是不明白为什么没有传递 List<Account>。我不知道我在这里做错了什么。
PanelNavigation.xaml
<!-- Navigation Section -->
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Grid.Row="1" Margin="0,0.333,0,0" Grid.ColumnSpan="2">
<UserControls:NavigationPanel DataContext="{Binding Accounts}"/>
</ScrollViewer>
<!-- Networth Panel -->
<Grid Grid.Row="2" Height="40">
<Border BorderThickness="0,1,0,0" BorderBrush="{StaticResource MenuBarBrush}" Background="{StaticResource BackgroundLightBrush}">
<Grid Margin="5,5,10,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock Margin="10,1,10,1" FontSize="16" FontWeight="Normal" Grid.Column="0" Foreground="#FF346C9C" Text="Net Worth" />
<TextBlock FontSize="16" FontWeight="Normal" Grid.Column="1"
HorizontalAlignment="Right" Text="{Binding NetWorth, Converter={StaticResource CurrencyConverter}}"
Foreground="{Binding NetWorth, Converter={StaticResource ChangeColor}}"/>
</Grid>
</Border>
</Grid>
PanelNavigation.xaml.cs
using APTest.ViewModels;
using System.Windows.Controls;
namespace APTest.Views
{
/// <summary>
/// Interaction logic for PanelNavigation.xaml
/// </summary>
public partial class PanelNavigation : Page
{
public PanelNavigation()
{
InitializeComponent();
this.DataContext = new VMNavigationPanel(this);
}
}
}
NavigationPanel.xaml
<UserControl x:Class="APTest.UserControls.NavigationPanel"
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:UserControls="clr-namespace:APTest.UserControls"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="400">
<StackPanel>
<StackPanel>
<ItemsControl x:Name="DataTest" ItemsSource ="{Binding Accounts}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding TotalBalance}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</StackPanel>
</UserControl>
NavigationPanel.xaml.cs
using System.Windows.Controls;
namespace APTest.UserControls
{
public partial class NavigationPanel : UserControl
{
public NavigationPanel()
{
InitializeComponent();
}
}
最后
VMNavigationPanel.cs
using AccountingPlus.ViewModels;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace APTest.ViewModels
{
class VMNavigationPanel : BaseViewModel
{
private Page mPage;
private ObservableCollection<Account> accounts;
public ObservableCollection<Account> Accounts {
get => accounts;
set {
accounts = value;
OnPropertyChanged();
}
}
public double NetWorth { get; set; } = 127492.30;
public VMNavigationPanel(Page page)
{
mPage = page;
Accounts = new ObservableCollection<Account>();
Account test = new Account();
test.TotalBalance = 100234.23;
Accounts.Add(test);
}
}
public class Account
{
public double TotalBalance {get; set;}
}
}
BaseViewModel.cs
using PropertyChanged;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace APTest
{
[AddINotifyPropertyChangedInterface]
class BaseViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = (Sender, e) => { };
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
【问题讨论】:
-
Itemssource 应该绑定 Accounts。 Observablecollection 而不是列表。始终在视图模型上实现 inotifypropertychanged 并在 setter 中引发 propertychanged。不要将页面等 ui 元素传递到视图模型中。
-
如果您不需要像向导那样向前向后导航,则框架和页面会产生开销。 social.technet.microsoft.com/wiki/contents/articles/…
-
@Andy 你能解释一下将 UI 元素(如页面)传递给 ViewModel 是什么意思吗?我有一个带有 UIControl 的页面,它通过 嵌入到 mainWindow 中,仍在争论这是否是一件好事。至于 INotifyPropertyChanged,我实际上有,但将其删除以进行测试并将其放回去没有任何区别,而 ObservableCollection 没有任何区别。
-
@AndyAnd 我还在学习,那么你推荐什么而不是框架/页面。举一个我想要达到的目标的例子。想想带有信息和链接列表的侧边导航。然后,这些链接可以将更多内容打开到主要内容部分。我知道我需要在这里使用一个框架,因为我没有看到任何其他方式来更改该主要内容区域,并且该页面必须具有 UserControls。
-
Vmnavigationpanel 是一个视图模型。它应该完全独立于 ui 工作,并且页面是 ui。我在上面的评论中放了一个链接。这不是随机的。阅读。然后首先谷歌视图模型。取字框。取词页。把它们放在一个盒子里。合上盖子..想想 contentcontrol 和 usercontrol。具有数据类型的数据模板定义了您为视图模型获得的用户界面。
标签: c# wpf xaml user-controls