2015-09-17 105 views
0

我正在學習WPF,我試圖做一些簡單的事情。我有兩個類:CandyMyColor。這兩個類的代碼如下所示WPF:在ComboBox中使用ItemsControl DataTemplate

public class Candy 
{ 
    public MyColor Color { get; set; } 
    public string Name { get; set; } 
} 

public class MyColor 
{ 
    public string Name { get; set; } 
    public uint Id { get; set; } 
} 

(我附上下面的圖像,以使其更清晰)

我在窗口的區域中,我可以通過使用文本框創建MyColor插入MyColor.Name,以及增加MyColor.Id的簡單邏輯。在窗口的另一面,我有一個按鈕,它在包含Candy的ItemsControl中創建新項目。在此ItemsControl中有一個ComboBox,我可以指定Candy.ColorTextBox,我指定Candy.Name。最後,當我按下按鈕生成列表代碼應在以下TextBox列表輸出的

格式

Candy.Color Candy.Name

Color Candy Maker

我想圖如何自動填充填充了我創建的顏色列表的ComboBox,以便我可以指定顏色Candy,但我不知道如何綁定我的數據源。另外,我將如何生成文本?

目前我的代碼看起來像這樣

namespace QuestionToAsk 
{ 
/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    ObservableCollection<MyColor> Colors; 
    ObservableCollection<Candy> Candies; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     Colors = new ObservableCollection<MyColor>(); 
     Candies = new ObservableCollection<Candy>(); 

     Colors.Add(new MyColor() { Name = "(Unspecified)", Id = 0 }); 

     icColors.ItemsSource = Colors; 
     icCandies.ItemsSource = Candies; 
    } 

    private void btnColor(object sender, RoutedEventArgs e) 
    { 
     if (txtColor.Text != "") 
     { 
      uint last_id = Colors.Last<MyColor>().Id; 
      Colors.Add(new MyColor() { Name = txtColor.Text, Id = last_id+1 }); 
      txtColor.Text = ""; 
     } 
    } 

    private void btnNewCandy(object sender, RoutedEventArgs e) 
    { 
     Candies.Add(new Candy()); 
    } 

    private void btnGetList(object sender, RoutedEventArgs e) 
    { 
     //How to create the list of <Color, Name>? 
    } 
} 

public class Candy 
{ 
    public MyColor Color { get; set; } 
    public string Name { get; set; } 
} 

public class MyColor 
{ 
    public string Name { get; set; } 
    public uint Id { get; set; } 
} 
} 

我的XML文件看起來像這樣:

<Window x:Class="QuestionToAsk.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:QuestionToAsk" 
     Title="Color Candy Maker" Height="350" Width="525"> 
    <Grid> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="*" /> 
     </Grid.RowDefinitions> 
     <DockPanel Margin="3"> 
      <Button Content="Add Color" Click="btnColor" DockPanel.Dock="Bottom"/> 
      <TextBox x:Name="txtColor" DockPanel.Dock="Bottom"/> 
      <ItemsControl x:Name="icColors" Grid.Column="0" Grid.Row="0" DockPanel.Dock="Top"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate x:Name="tColorsTemplate"> 
         <TextBlock Text="{Binding Name}" Name="Color" /> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </DockPanel> 
     <DockPanel Grid.Column="1" Grid.Row="0" Margin="3"> 
      <Grid DockPanel.Dock="Bottom"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <Button Content="New Candy" Click="btnNewCandy" Grid.Column="0"/> 
       <Button Content="Generate List" Click="btnGetList" Grid.Column="1"/> 
      </Grid> 
      <ItemsControl Name="icCandies" DockPanel.Dock="Top"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <ComboBox Name="cmbColors" Grid.Column="0"> 
           <!-- How to bind this cmbColors to icColors? --> 
          </ComboBox> 
          <TextBox Text="{Binding Name}" Grid.Column="1" /> 
         </Grid> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
     </DockPanel> 
     <DockPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> 
      <TextBox x:Name="txtColorCandy"/> 
     </DockPanel> 
    </Grid> 
</Window> 

回答

0

編輯

我已刪除了我所有的想法和使用您的實現了它硬設計:D。

的XAML代碼

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
    <DockPanel Margin="3"> 
     <Button Content="Add Color" Click="btnColor" DockPanel.Dock="Bottom"/> 
     <TextBox x:Name="txtColor" DockPanel.Dock="Bottom"/> 
     <ItemsControl x:Name="icColors" Grid.Column="0" Grid.Row="0" DockPanel.Dock="Top"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate x:Name="tColorsTemplate"> 
        <TextBlock Text="{Binding Name}" Name="Color" /> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </DockPanel> 
    <DockPanel Grid.Column="1" Grid.Row="0" Margin="3"> 
     <Grid DockPanel.Dock="Bottom"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="*" /> 
      </Grid.ColumnDefinitions> 
      <Button Content="New Candy" Click="btnNewCandy" Grid.Column="0"/> 
      <Button Content="Generate List" Click="btnGetList" Grid.Column="1"/> 
     </Grid> 
     <ItemsControl Name="icCandies" DockPanel.Dock="Top"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*" /> 
          <ColumnDefinition Width="*" /> 
         </Grid.ColumnDefinitions> 
         <ComboBox Name="cmbColors" Grid.Column="0" 
            ItemsSource="{Binding Colors, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 
            DisplayMemberPath="Name" 
            SelectedItem="{Binding Color}"> 
         </ComboBox> 
         <TextBox Text="{Binding Name}" Grid.Column="1" /> 
        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </DockPanel> 
    <DockPanel Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"> 
     <TextBox x:Name="txtColorCandy" VerticalScrollBarVisibility="Auto"/> 
    </DockPanel> 
</Grid> 

RelativeSource由於ComboBox值的使用是在Window類的屬性,而所選擇的值是要被保存在Candy類。所以ItemsSource勢必Window類的屬性和SelectedItem綁定到Candy

碼的性能背後

public partial class MainWindow : Window 
{ 
    private ObservableCollection<MyColor> _colors; 

    public IEnumerable<MyColor> Colors 
    { 
     get { return _colors; } 
    } 

    private ObservableCollection<Candy> _candies; 

    public IEnumerable<Candy> Candies 
    { 
     get { return _candies; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     _colors = new ObservableCollection<MyColor>(); 
     _candies = new ObservableCollection<Candy>(); 

     _colors.Add(new MyColor { Name = "(Unspecified)", Id = 0 }); 
     icColors.ItemsSource = Colors; 
     icCandies.ItemsSource = Candies; 
    } 

    private void btnColor(object sender, RoutedEventArgs e) 
    { 
     if (txtColor.Text != "") 
     { 
      uint last_id = Colors.Last<MyColor>().Id; 
      _colors.Add(new MyColor() { Name = txtColor.Text, Id = last_id + 1 }); 
      txtColor.Text = ""; 
     } 
    } 

    private void btnNewCandy(object sender, RoutedEventArgs e) 
    { 
     _candies.Add(new Candy()); 
    } 

    private void btnGetList(object sender, RoutedEventArgs e) 
    { 
     StringBuilder sb = new StringBuilder(); 
     foreach (var item in _candies) 
     { 
      if (item.Name == null || item.Color == null) 
       continue; 
      sb.AppendLine(item.Color.Name + " " + item.Name); 
     } 

     txtColorCandy.Text = sb.ToString(); 
    } 
} 

它有任何困惑,讓我知道,我會盡力幫助

+0

嗨@sandesh,我注意到你所做的改變並不是我想要的。我希望當我點擊「New Candy」時,我可以在窗口右上角的ItemsControl中創建一個新項目,這樣我就可以看到每種顏色的糖果。 – silentwf

+0

嗨,我沒有能夠測試新的,但我能夠自己解決它。該解決方案與您所提議的類似。謝謝! :) – silentwf

相關問題