我正在制作一个演示应用程序,其中一个简单的类Fruit具有两个属性Name和Color,并显示在FruitUC中,每个属性都有一个字段。在ProduceVM类中,我用Fruit示例填充ObservableCollection SupportedFruits,并将ProduceVM示例绑定到另一个用户控件FruitView,其中ItemsControl显示SupportedFruits的每个元素。我还有一个ProduceVM的MockProduceVM子类,它创建并添加了3个Fruit对象,我应该在设计时看到的SupportedFruits。
在运行时一切都很好,但在设计时FruitUC是空的。如果在ItemsControl中我有显示每个水果的名称和颜色的TextBox,它们模拟得很好。但每个FruitUC应该绑定到相同的数据,就像两个属性都是空字符串一样。我在FruitView的ItemsControl中缺少FruitUC什么?提前感谢,并为一些这种格式感到抱歉...
水果:
public class Fruit : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private string _name;
private string _color;
public string Name {
get { return _name; }
set {
if (_name != value) {
_name = value;
NotifyListeners();
}
}
}
public string Color {
get { return _color; }
set {
if (_color != value) {
_color = value;
NotifyListeners();
}
}
}
private void NotifyListeners([CallerMemberName] string propertyName = "") {
try {
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
} catch (Exception anException) {
throw anException;
}
}
字符串
FruitUC.xaml:
<UserControl x:Class="WPFInAction.FruitUC"
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:WPFInAction"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid Background="LightGray" d:DataContext="{d:DesignInstance Type=local:Fruit, IsDesignTimeCreatable=True}">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="20" />
<Setter Property="HorizontalAlignment" Value="Right" />
<Setter Property="Margin" Value="5,1,5,0" />
</Style>
<Style TargetType="TextBox">
<Setter Property="MinWidth" Value="80" />
<Setter Property="Height" Value="30" />
<Setter Property="FontSize" Value="20" />
</Style>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="10" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock x:Name="lblFruitName" Text="Name:" Grid.Row="1" />
<TextBox x:Name="txtFruitName" Text="{Binding Name}" Grid.Row="1" Grid.Column="1" />
<TextBlock x:Name="lblFruitColor" Text="Color:" Grid.Row="2" />
<TextBox x:Name="txtFruitColor" Text="{Binding Color}" Grid.Row="2" Grid.Column="1" />
</Grid>
型
FruitUC.xaml.cs:
public partial class FruitUC : UserControl {
public FruitUC() {
InitializeComponent();
}
}
型
ProduceVM:
public class ProduceVM {
public ObservableCollection<Fruit> SupportedFruits { get; set; }
public ProduceVM() {
SupportedFruits = new ObservableCollection<Fruit>();
private static readonly string[] _fruitNames = { "Apple", "Orange", "Banana", "Avocado", "Blueberry", "Grape" };
private static readonly string[] _fruitColors = { "Red", "Orange", "Yellow", "Green", "Blue", "Purple" };
if (!DesignerProperties.GetIsInDesignMode(new System.Windows.DependencyObject())) {
for (var f = 0; f < _fruitNames.Length; f++) {
SupportedFruits.Add(new Fruit { Name = _fruitNames[f], Color = _fruitColors[f] });
}
}
}
}
型
MockProduceVM:
public class MockProduceVM : ProduceVM {
public MockProduceVM() : base() {
SupportedFruits.Add(new Fruit { Name ="Grape", Color = "Red" });
SupportedFruits.Add(new Fruit { Name ="Apple", Color = "Yellow" });
SupportedFruits.Add(new Fruit { Name ="Banana", Color = "Green" });
}
}
型
FruitsView.xaml:
<UserControl x:Class="WPFInAction.FruitsView"
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:WPFInAction"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<DockPanel d:DataContext="{d:DesignInstance Type=local:MockProduceVM, IsDesignTimeCreatable=True}" Background="AliceBlue">
<ItemsControl ItemsSource="{Binding SupportedFruits}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel Orientation="Vertical">
<local:FruitUC />
<TextBox x:Name="txtFruitName" Text="{Binding Name}" Width="70" />
<TextBox x:Name="txtFruitColor" Text="{Binding Color}" Width="70" Margin="10,0,0,10" />
</WrapPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DockPanel>
型
FruitsView.xaml.cs:
public partial class FruitsView : UserControl {
public FruitsView() {
InitializeComponent();
DataContext = new ProduceVM();
}
}
型
运行时:x1c 0d1x
在设计/模拟时:
的
编辑:我注意到,如果我对MockProduceVM进行更改,然后构建,然后转到FruitsView.xaml,我会看到设置的属性值,然后在一瞬间它们就清空了。
编辑2:如果我用FruitUC的内容替换FruitUC中的FruitView,Name和Color字段仍然显示为空白。
1条答案
按热度按时间n6lpvg4x1#
在
FreuitUC.xaml
中,您将在设计时覆盖Grid
的Datacontext
,<Grid Background="LightGray" d:DataContext="{d:DesignInstance Type=local:Fruit, IsDesignTimeCreatable=True}">
个所以在
Grid
内部,DataContext
不再是SupportedFruits
集合的集合项。删除
DataContext
覆盖,它应该是好的。的数据