2017-02-11 64 views
0

我已經建立了WPF有幾個列表框和添加按鈕:到選擇列表框添加項按鈕點擊

<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" 
    xmlns:self="clr-namespace:QuickSlide_2._0" 
    xmlns:e="http://schemas.microsoft.com/expression/2010/interactivity" 
    Title="Load_style" Height="300" Width="300" MinHeight="720" MinWidth="1280" WindowStyle="None" AllowsTransparency="True" Background="#B0000000" AllowDrop="True" WindowStartupLocation="CenterScreen" ShowInTaskbar="False"> 

    <Grid> 

    <Rectangle Height="720" HorizontalAlignment="Left" Name="rectangle1" Stroke="#00000000" VerticalAlignment="Top" Width="1280" MinHeight="320" MinWidth="380" Fill="DarkGray"/> 


    <ListBox Height="241" HorizontalAlignment="Left" Margin="502,371,0,0" Name="Presentation_slide_items" VerticalAlignment="Top" Width="199" /> 

    <ListBox Name="subjects_list" Margin="74,154,1039,171" ItemsSource="{Binding ElementName=styles_list, Path=SelectedItem.subjects}"/> 
    <ListBox Name="sub_subjects_list" Margin="264,154,849,171" ItemsSource="{Binding ElementName=subjects_list, Path=SelectedItem.sub_subjects}"/> 
    <ComboBox x:Name="styles_list" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="74,112,0,0"/> 
    <ListBox Name="user_inputs" Margin="502,154,565,421" ItemsSource="{Binding ElementName=sub_subjects_list, Path=SelectedItem.possible_input, Mode=TwoWay}" > 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBox Name="TextBoxList" Text="{Binding input}" BorderThickness="0" />      
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <Button x:Name="button_add_input" Content="Add" HorizontalAlignment="Left" Margin="502,279,0,0" VerticalAlignment="Top" Width="106" Command="{Binding ElementName=sub_subjects_list.add_input} /> 
</Grid> 

現在我想的更多的USER_INPUT在user_inputs列表框中添加到列表當點擊按鈕「button_add_input」時。我一直在尋找,它看起來像使用按鈕的「命令」選項可能是要走的路。 這是我的班「sub_subject」

 public class sub_subject 
{ 
    public string short_name { get; set; } 
    public string name { get; set; } 
    public bool read_from_db { get; set; } 
    public string table_name { get; set; } 
    //public ObservableCollection<string> possible_input { get; set; } 
    public ObservableCollection<possible_input> possible_input { get; set; } 

    public sub_subject(string name) 
    { 
     this.name = name; 

     possible_input = new ObservableCollection<possible_input>(); 
     //possible_input = new ObservableCollection<string>(); 
    } 
    public override string ToString() 
    { 
     return this.name; 
    } 
    public void add_input() 
    { 
     possible_input input = new possible_input(); 
     input.input = ""; 
     possible_input.Add(input); 
    } 
} 

我想我可以添加一個功能,增加了一個possible_input到的ObservableCollection類並調用按鈕的命令這一功能。但我無法弄清楚如何設置正確的命令。有什麼建議麼?

回答

0

我假設sub_subject是窗口(視圖)的viewmodel(datacontext)。

在這種情況下,您應該爲您的項目添加一個Command類。這是ICommand的一個實現。你可以在這裏找到一個ICommand的實現:https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

之後,你必須在你的viewmodel中爲調用add_input方法的命令創建一個屬性。然後,將您的命令屬性綁定到該按鈕的Command屬性。

+0

感謝Niels的反應。這看起來像要走的路,會試一試! –

相關問題