首页 文章

C#将列表框中的项添加到Usercontrol

提问于
浏览
1

我是一名新程序员 . 我会尽力解释 . 我正在编写WPF中的工具 . 我按如下方式进行了用户控制 .

User control

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"></ColumnDefinition>
        <ColumnDefinition Width="150"></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition Width="50"></ColumnDefinition>
        <ColumnDefinition Width="30"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Button x:Name="DelOutConBT" Margin="5" Content="X" Click=" Delete ConnSet"></Button>
    <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>

在主窗口中,我已经编写了一个列表框和一个选项卡 . 列表框将填充一些运行Python脚本的项目 .

现在,我想通过选择列表框中的项目并单击主窗口中的按钮来启用用户添加用户控件 .

我的问题是usercontrol如何在添加时自动加载信息 . 我希望Usercontrol中的Label显示从列表框中选择的项目,Combobox显示列表框中的所有项目 .

然后,通过单击usercontrol中的“>>”按钮,可以将usercontrol中的数据保存到类或其他位置 . 因为我将使用另一个Python脚本填充usercontrols中的所有数据 .

这是我编写的将用户控件添加到主窗口的程序 .

Mainwindow.xaml.cs

private void ConSel_Click(object sender, RoutedEventArgs e)
{
    if (ListB.SelectedItem != null)
    {
        string name = ((ListBoxItem)ListB.SelectedItem).Name;                
        ConnSet CSobj = new ConnSet();
        StkConn.Children.Add(CSobj);                
    }                        
}

1 回答

  • 0

    我认为你所寻找的是列表视图的“风格” . 你的问题不是很清楚,所以我不能准确地给你你想要的东西,但是这里有一个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>
    

    Code Behind:

    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
        }                        
    }
    

    快速编辑:xaml代码中发生的事情是,它表示“对于每个项目,请按照此模板进行设计”,因此当添加新项目时,它会查看该模板并为您提供所有内容 .

    请注意,如果您使用WPF,我建议您查看MVVM或MVC模式 . 从我所看到的,这就是WPF的设计使用方式 . 有很多关于这个主题的好书,你可以查看 . 希望这有帮助!

相关问题