gpt4 book ai didi

c# - WPF 项目控制 : Can't seem to get items to display in the view

转载 作者:行者123 更新时间:2023-11-30 14:20:17 26 4
gpt4 key购买 nike

我正在处理的代码是 WPF 应用程序的一部分,该应用程序应显示一组用户可以与之交互的扑克牌。所以我正在使用三个 UserControl:

  • 主控
  • CardStackControl
  • 卡片控制

在 MainControl.xaml 中:

    <StackPanel 
Grid.Row="0"
Orientation="Horizontal">
<ItemsControl
ItemsSource="{Binding Path=CardStacks}"
>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl>
</StackPanel>

我的数据模板在 Resources.xaml 文件中定义:

<DataTemplate x:Key="MainTemplate" DataType="{x:Type ViewModel:MainViewModel}">
<View:MainControl />
</DataTemplate>

<DataTemplate DataType="{x:Type ViewModel:CardStackViewModel}">
<View:CardStackControl />
</DataTemplate>

<DataTemplate x:Key="CardTemplate" DataType="{x:Type ViewModel:CardViewModel}">
<View:CardControl />
</DataTemplate>

CardStackControl.xaml:

<StackPanel Orientation="Vertical">
<TextBlock
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=CardName}"
/>
<ItemsControl
Height="Auto"
ItemsSource="{Binding Path=Cards}"
ItemTemplate="{StaticResource CardTemplate}"
>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl>
<TextBlock
Height="30"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=Count}"
/>
</StackPanel>

卡片控件.xaml:

    <TextBlock 
Name="TitleTextBlock"
Grid.Row="0"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding Path=Name}"
>

主视图模型:

public ObservableCollection<CardStackViewModel> CardStacks;

// ...

CardStackViewModel:

public class CardStackViewModel
{
private CardStack Model;

public string CardName
{
get
{
return this.Model.CardType.Name;
}
}

public int Count
{
get
{
return this.Model.Cards.Count;
}
}

public ObservableCollection<CardViewModel> Cards { get; private set; }

// ...

卡片 View 模型:

    /// <summary>
/// A reference to the Card type.
/// </summary>
private Card Model;

/// <summary>
/// Retrieves the name of the card.
/// </summary>
public string Name
{
get
{
return this.Model.CardType.Name;
}
}

public string Caption
{
get
{
return this.Model.CardType.Text;
}
}

// ...

在 MainViewModel、CardStackViewModel 构造函数中,两个 ObservableCollection 实例都被初始化、填充并填充了一些测试数据。

当我加载应用程序并创建 MainViewModel 时,UserControls 不显示,并且没有任何测试数据可见。我正在使用 this tutorial作为指南,以便 MainViewModel 对应于该窗口中选项卡式控件上的“工作区”ViewModel。当然,此示例应水平布局每个控件,目前这很好 - 我仍在学习 WPF。

显然,我已经在 .xaml 文件和其他脚手架代码中删除了代码生成的标签(如果这些示例中的代码不清楚,我可以修改它以发布该代码)。我现在只测试/绑定(bind)到模型类上的字符串属性。

最佳答案

您声明 ItemsPanelTemplate 的方式不正确:您将其声明为 ItemsControl 的一个项目。你应该这样做:

    <ItemsControl 
ItemsSource="{Binding Path=CardStacks}"
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" IsItemsHost="True" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>

关于c# - WPF 项目控制 : Can't seem to get items to display in the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1475169/

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