wpf ItemsControl中的用户控件不填充模拟属性

nzk0hqpo  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(63)

我正在制作一个演示应用程序,其中一个简单的类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字段仍然显示为空白。

n6lpvg4x

n6lpvg4x1#

FreuitUC.xaml中,您将在设计时覆盖GridDatacontext
<Grid Background="LightGray" d:DataContext="{d:DesignInstance Type=local:Fruit, IsDesignTimeCreatable=True}">
所以在Grid内部,DataContext不再是SupportedFruits集合的集合项。
删除DataContext覆盖,它应该是好的。


的数据

相关问题