Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 将项目添加到ItemsControl_Wpf_Itemscontrol - Fatal编程技术网

Wpf 将项目添加到ItemsControl

Wpf 将项目添加到ItemsControl,wpf,itemscontrol,Wpf,Itemscontrol,我有一份电话申请。每个呼叫(线路)都有自己独特的信息。说一个彩色图标加上行号。数字在队列中,我使用并行编程技巧处理这些项目。处理项目时,信息将显示在屏幕上。这里我更喜欢ItemsControl 预期结果与图像相似。我想 我借用了手机图标的代码 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schema

我有一份电话申请。每个呼叫(线路)都有自己独特的信息。说一个彩色图标加上行号。数字在队列中,我使用并行编程技巧处理这些项目。处理项目时,信息将显示在屏幕上。这里我更喜欢ItemsControl

预期结果与图像相似。我想

我借用了手机图标的代码

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                   >
<SolidColorBrush x:Key="RedBrush" Color="Red" />
<SolidColorBrush x:Key="AmberBrush" Color="#FFFFC500" />
<SolidColorBrush x:Key="GreenBrush" Color="Green" />
<Geometry x:Key="PhoneIcon">F1M52.5221,11.1016C52.0637,10.6796 44.4973,4.55737 29.4347,12.7369 26.3098,14.4296 23,17.224 20.095,20.1692 17.1497,23.073 14.3555,26.3842 12.6626,29.509 4.48303,44.5703 10.6093,52.1393 11.0298,52.5962 11.0298,52.5962 12.9778,55.5885 14.7057,53.8579L23.3555,45.2134C24.897,43.6692 22.7721,39.2134 18.2563,39.3541 17.1301,39.3906 15.5481,38.9531 17.0571,35.5156 18.3945,32.4623 22.3436,27.4766 24.8879,24.9648 27.4048,22.418 32.3904,18.4713 35.4426,17.1301 38.8787,15.6223 39.3175,17.2031 39.2811,18.332 39.1393,22.8462 43.5962,24.9686 45.1366,23.4283L53.7864,14.7787C55.5142,13.052,52.5221,11.1016,52.5221,11.1016z</Geometry>
我将ItemsControl定义为:

<DockPanel>  
        <ItemsControl Height="300">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="40"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>

不确定下一步是什么?

假设您在这里不使用MVVM,只使用带有代码隐藏类的XAML表单-您需要先稍微更改代码隐藏。您需要一个对象来表示“线”-并使颜色成为画笔,而不是字符串:

public class Line
{
    public string LineNumber { get; set; }
    public System.Windows.Media.Brush LineColour { get; set; }
}
然后,您需要代码来构建这些项的集合,ItemsControl可以显示这些项。它可能看起来像这样:

public partial class MainWindow : Window
{
    private List<Line> _lines;

    public MainWindow()
    {
        InitializeComponent();

        _lines = new List<Line>();

        // you can swap this for iterating around a database query or whatever you use to store the lines / calls
        _lines.Add(new Line() { LineNumber = "line1", LineColour = new SolidColorBrush(Colors.Red) });
        _lines.Add(new Line() { LineNumber = "line2", LineColour = new SolidColorBrush(Colors.Green) });
        _lines.Add(new Line() { LineNumber = "line3", LineColour = new SolidColorBrush(Color.FromRgb(255, 188, 59)) });

        // this part binds this list to your itemsControl
        items.ItemsSource = _lines;
    }
公共部分类主窗口:窗口
{
私有列表行;
公共主窗口()
{
初始化组件();
_行=新列表();
//您可以将其交换为在数据库查询或用于存储行/调用的任何内容上进行迭代
_添加(newline(){LineNumber=“line1”,linecolor=newsolidcolorbush(Colors.Red)});
_添加(newline(){LineNumber=“line2”,linecolor=newsolidcolorbush(Colors.Green)});
_添加(newline(){LineNumber=“line3”,linecolor=newsolidcolorbush(Color.FromRgb(255,188,59)));
//此部分将此列表绑定到itemsControl
items.ItemsSource=\u行;
}
那么您的XAML就相当简单了,您只需定义一个名为“items”的ItemsControl并定义一个将应用于每个项目的样式。这将包含行号,将画笔应用于图像,您将创建一个“路径”:


如果您使用的是MVVM,那么一切都仍然适用,但是您不需要在代码隐藏中分配ItemsSource,而是要绑定到视图模型的_lines集合——如果您想动态添加/删除,您可以将该集合设置为ObservableCollection而不是List


但是我给了你一个最简单的例子,因为这就是你最初的问题所建议的!

你应该先学习WPF中的所有基本概念(包括MVVM模式)。我知道这并不容易,但这是一个必须要做的开始。一旦熟悉了概念,你可以编写自己的演示WPF应用程序,尽管还有许多意想不到的结果等着你。WPF确实有一个陡峭的学习曲线,所以在你感到轻松之前要耐心。
public partial class MainWindow : Window
{
    private List<Line> _lines;

    public MainWindow()
    {
        InitializeComponent();

        _lines = new List<Line>();

        // you can swap this for iterating around a database query or whatever you use to store the lines / calls
        _lines.Add(new Line() { LineNumber = "line1", LineColour = new SolidColorBrush(Colors.Red) });
        _lines.Add(new Line() { LineNumber = "line2", LineColour = new SolidColorBrush(Colors.Green) });
        _lines.Add(new Line() { LineNumber = "line3", LineColour = new SolidColorBrush(Color.FromRgb(255, 188, 59)) });

        // this part binds this list to your itemsControl
        items.ItemsSource = _lines;
    }
    <!-- this bit defines how each item looks-->
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="100"/>
                </Grid.ColumnDefinitions>

                <Path Grid.Column="0" Fill="{Binding LineColour}"  Data="F1M52.5221,11.1016C52.0637,10.6796 44.4973,4.55737 29.4347,12.7369 26.3098,14.4296 23,17.224 20.095,20.1692 17.1497,23.073 14.3555,26.3842 12.6626,29.509 4.48303,44.5703 10.6093,52.1393 11.0298,52.5962 11.0298,52.5962 12.9778,55.5885 14.7057,53.8579L23.3555,45.2134C24.897,43.6692 22.7721,39.2134 18.2563,39.3541 17.1301,39.3906 15.5481,38.9531 17.0571,35.5156 18.3945,32.4623 22.3436,27.4766 24.8879,24.9648 27.4048,22.418 32.3904,18.4713 35.4426,17.1301 38.8787,15.6223 39.3175,17.2031 39.2811,18.332 39.1393,22.8462 43.5962,24.9686 45.1366,23.4283L53.7864,14.7787C55.5142,13.052,52.5221,11.1016,52.5221,11.1016z"/>
                <TextBlock Grid.Column="1" Text="{Binding LineNumber}"/>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <!-- this bit defines the list appearance, we'll go vertical in a stack panel! -->
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>