C#将项目从列表框添加到用户控件

C#将项目从列表框添加到用户控件,c#,wpf,user-controls,C#,Wpf,User Controls,我是一个新的程序员。我会尽力解释清楚。我正在用WPF编写一个工具。我制作了一个usercontrol,如下所示 我认为您需要的是listview的“样式”。您的问题不是很清楚,因此我无法确切地告诉您您要查找的内容,但下面是一个使用绑定的ListView示例,它可以满足您的需要: UI: <ListBox ItemsSource="{Binding Items}" > <ListBox.ItemTemplate> <DataTemplate&

我是一个新的程序员。我会尽力解释清楚。我正在用WPF编写一个工具。我制作了一个usercontrol,如下所示


我认为您需要的是listview的“样式”。您的问题不是很清楚,因此我无法确切地告诉您您要查找的内容,但下面是一个使用绑定的ListView示例,它可以满足您的需要:

UI:

<ListBox ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="DelOutConBT" Margin="5" Content="X" Click=" Delete ConnSet"/>
                <Label x:Name="OutConLB" Grid.Column="1" ></Label>
                <ComboBox x:Name="InConComB" Grid.Column="2"></ComboBox>
                <TextBox x:Name="ConnValueTX" Grid.Column="3" Margin="5"></TextBox>
                <Button Grid.Column="4" Margin="5" Content=">>"></Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public ObservableCollection<ConnSet> Items { get; set; }

private void ConSel_Click(object sender, RoutedEventArgs e)
{
    if (ListB.SelectedItem != null)
    {
        string name = ((ListBoxItem)ListB.SelectedItem).Name;                
        ConnSet CSobj = new ConnSet();
        Items.Add(CSobj); // with the correct bindings, this will push to the UI for you
    }                        
}

代码隐藏:

<ListBox ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="DelOutConBT" Margin="5" Content="X" Click=" Delete ConnSet"/>
                <Label x:Name="OutConLB" Grid.Column="1" ></Label>
                <ComboBox x:Name="InConComB" Grid.Column="2"></ComboBox>
                <TextBox x:Name="ConnValueTX" Grid.Column="3" Margin="5"></TextBox>
                <Button Grid.Column="4" Margin="5" Content=">>"></Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public ObservableCollection<ConnSet> Items { get; set; }

private void ConSel_Click(object sender, RoutedEventArgs e)
{
    if (ListB.SelectedItem != null)
    {
        string name = ((ListBoxItem)ListB.SelectedItem).Name;                
        ConnSet CSobj = new ConnSet();
        Items.Add(CSobj); // with the correct bindings, this will push to the UI for you
    }                        
}
publicobservableCollection项{get;set;}
私有void ConSel_单击(对象发送方,路由目标)
{
如果(ListB.SelectedItem!=null)
{
字符串名称=((ListBoxItem)ListB.SelectedItem).name;
ConnSet CSobj=新的ConnSet();
Items.Add(CSobj);//使用正确的绑定,这将为您推送到UI
}                        
}
快速编辑:xaml代码中的内容是“对于每个项目,按照此模板进行设计”,因此当添加新项目时,它会查看该模板并为您安排所有内容


注意,如果您使用WPF,我建议您研究MVVM或MVC模式。据我所见,这就是WPF的设计用途。有很多关于这个主题的好书,你可以看看。希望这有帮助

我认为您需要的是listview的“样式”。您的问题不是很清楚,因此我无法确切地告诉您您要查找的内容,但下面是一个使用绑定的ListView示例,它可以满足您的需要:

UI:

<ListBox ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="DelOutConBT" Margin="5" Content="X" Click=" Delete ConnSet"/>
                <Label x:Name="OutConLB" Grid.Column="1" ></Label>
                <ComboBox x:Name="InConComB" Grid.Column="2"></ComboBox>
                <TextBox x:Name="ConnValueTX" Grid.Column="3" Margin="5"></TextBox>
                <Button Grid.Column="4" Margin="5" Content=">>"></Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public ObservableCollection<ConnSet> Items { get; set; }

private void ConSel_Click(object sender, RoutedEventArgs e)
{
    if (ListB.SelectedItem != null)
    {
        string name = ((ListBoxItem)ListB.SelectedItem).Name;                
        ConnSet CSobj = new ConnSet();
        Items.Add(CSobj); // with the correct bindings, this will push to the UI for you
    }                        
}

代码隐藏:

<ListBox ItemsSource="{Binding Items}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition Width="150"/>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="50"/>
                    <ColumnDefinition Width="30"/>
                </Grid.ColumnDefinitions>
                <Button x:Name="DelOutConBT" Margin="5" Content="X" Click=" Delete ConnSet"/>
                <Label x:Name="OutConLB" Grid.Column="1" ></Label>
                <ComboBox x:Name="InConComB" Grid.Column="2"></ComboBox>
                <TextBox x:Name="ConnValueTX" Grid.Column="3" Margin="5"></TextBox>
                <Button Grid.Column="4" Margin="5" Content=">>"></Button>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
public ObservableCollection<ConnSet> Items { get; set; }

private void ConSel_Click(object sender, RoutedEventArgs e)
{
    if (ListB.SelectedItem != null)
    {
        string name = ((ListBoxItem)ListB.SelectedItem).Name;                
        ConnSet CSobj = new ConnSet();
        Items.Add(CSobj); // with the correct bindings, this will push to the UI for you
    }                        
}
publicobservableCollection项{get;set;}
私有void ConSel_单击(对象发送方,路由目标)
{
如果(ListB.SelectedItem!=null)
{
字符串名称=((ListBoxItem)ListB.SelectedItem).name;
ConnSet CSobj=新的ConnSet();
Items.Add(CSobj);//使用正确的绑定,这将为您推送到UI
}                        
}
快速编辑:xaml代码中的内容是“对于每个项目,按照此模板进行设计”,因此当添加新项目时,它会查看该模板并为您安排所有内容


注意,如果您使用WPF,我建议您研究MVVM或MVC模式。据我所见,这就是WPF的设计用途。有很多关于这个主题的好书,你可以看看。希望这有帮助

首先,谢谢你的帮助。我试图解释得更清楚些。我不希望在列表框中添加usercontrol。我想将其添加到选项卡中。主窗口中的列表框和选项卡已完成。我的问题是UI的Combobox如何从列表框中加载所有项目,标签如何加载从列表框中选择的项目。希望情况会更清楚。我想我更了解你。。我想你要做的是在第二个标签中放一个“StackPanel”和一个名字,然后把控件放在里面。然后:ControlHost.Children.Add(CSobj);如果这回答了您的问题,请您将其标记为“已回答”,我可以在我的回答中添加第二条评论,以便其他人可以从解决方案中获益。首先,感谢您的帮助。我试图解释得更清楚些。我不希望在列表框中添加usercontrol。我想将其添加到选项卡中。主窗口中的列表框和选项卡已完成。我的问题是UI的Combobox如何从列表框中加载所有项目,标签如何加载从列表框中选择的项目。希望情况会更清楚。我想我更了解你。。我想你要做的是在第二个标签中放一个“StackPanel”和一个名字,然后把控件放在里面。然后:ControlHost.Children.Add(CSobj);如果这回答了您的问题,请您将其标记为“已回答”,我可以在我的答案中添加第二条注释,以便其他人可以从解决方案中获益?您应该使用ItemsControl中的TemplateSelector来执行此操作。这样做相对容易。您应该在ItemsControl中使用TemplateSelector来执行此操作。这样做相对容易。