Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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
C# 将项目添加到列表框的选定项目集合_C#_Wpf_Xaml_Listbox - Fatal编程技术网

C# 将项目添加到列表框的选定项目集合

C# 将项目添加到列表框的选定项目集合,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我的窗口中有两个列表框。第一个列表框“所有样式”显示集合中的样式,第二个列表框“样式主题”显示第一个列表框中选定样式集合中的主题。到目前为止还不错 我想引入一个按钮,将新主题添加到当前选定的样式中。是否有方法将按钮绑定到列表框的选定样式,并向绑定的选择添加新主题?这是我目前的代码: xaml: <Window x:Class="QuickSlide_2._0.Window1" x:Name="load_style" xmlns="http://schemas.microso

我的窗口中有两个列表框。第一个列表框“所有样式”显示集合中的样式,第二个列表框“样式主题”显示第一个列表框中选定样式集合中的主题。到目前为止还不错

我想引入一个按钮,将新主题添加到当前选定的样式中。是否有方法将按钮绑定到列表框的选定样式,并向绑定的选择添加新主题?这是我目前的代码:

xaml:

<Window x:Class="QuickSlide_2._0.Window1"
    x:Name="load_style"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Load_style" Height="300" Width="300" MinHeight="720" MinWidth="1280" WindowStyle="None" AllowsTransparency="True" Background="#B0000000" AllowDrop="True" WindowStartupLocation="CenterScreen" ShowInTaskbar="False">
<Grid>
    <Button Content="Close" Height="23" HorizontalAlignment="Left" Margin="1081,65,0,0" Name="close" VerticalAlignment="Top" Width="120" Click="close_Click" />
    <Button Content="New style" Height="23" HorizontalAlignment="Left" Margin="326,65,0,0" Name="New_style" VerticalAlignment="Top" Width="120" Click="New_style_Click"/>
    <Button Content="New subject" Height="23" HorizontalAlignment="Left" Margin="704,65,0,0" Name="New_subject" VerticalAlignment="Top" Width="120" Click="New_subject_Click" />
    <ListBox Height="146" HorizontalAlignment="Left" Margin="74,111,0,0" x:Name="all_styles" VerticalAlignment="Top" Width="209" ItemsSource="{Binding styles, ElementName=load_style}" SelectedIndex="0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <ListBox Height="314" HorizontalAlignment="Left" Margin="74,297,0,0" Name="Style_subjects" VerticalAlignment="Top" Width="209" ItemsSource="{Binding SelectedItem.subjects, ElementName=all_styles}" SelectedIndex="0">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding name}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

以及守则:

public class subject
{
    public string name {get; set;}
    public subject(string name)
    {
        this.name = name;
    }
}


public class style
{
    public string name { get; set; }
    public ObservableCollection<subject> subjects {get; set;}

    public style(string name)
    {
        this.name = name;
        subjects = new ObservableCollection<subject>();
    }
}


public partial class Window1 : Window
{
    public ObservableCollection<style> styles {get; set;}
    public Window1()
    {
        styles = new ObservableCollection<style>();
        InitializeComponent();

        styles.Add(new style("test"));
        styles[0].subjects.Add(new subject("item"));
   }

    private void close_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    private void New_style_Click(object sender, RoutedEventArgs e)
    {
        styles.Add(new style("new_style"));           
    }

    private void New_subject_Click(object sender, RoutedEventArgs e)
    {
        // this next line is probably not correct....
        this.add(new subject("new_subject"));
    }

}
公共类科目
{
公共字符串名称{get;set;}
公共主题(字符串名称)
{
this.name=名称;
}
}
公课风格
{
公共字符串名称{get;set;}
公共可观察集合主题{get;set;}
公共样式(字符串名称)
{
this.name=名称;
受试者=新的可观察集合();
}
}
公共部分类Window1:Window
{
公共可观察集合样式{get;set;}
公共窗口1()
{
styles=新的ObservableCollection();
初始化组件();
添加(新样式(“测试”);
样式[0]。主题。添加(新主题(“项目”);
}
私有无效关闭\单击(对象发送者,路由目标)
{
这个。关闭();
}
私有无效新建样式单击(对象发送者,路由目标)
{
添加(新样式(“新样式”);
}
私有无效新建主题单击(对象发送者,路由目标)
{
//下一行可能不正确。。。。
本条添加(新主题(“新主题”);
}
}

谢谢,这正是我想要的!
((style)this.all_styles.SelectedItem).Add(new subject("item"));