我在XAML如下:如何通過後面的代碼使用DataBinding?
<StackPanel Orientation="Horizontal" Margin="0 5 0 0" HorizontalAlignment="Center" VerticalAlignment="Bottom">
<TextBox Text="LinkColor" VerticalAlignment="Center" IsReadOnly="True"/>
<ComboBox x:Name="ColorCombo" MinWidth="180" Margin="5 0 0 0" SelectionChanged="ColorCombo_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Key}" VerticalAlignment="Center" Height="10" Width="20"/>
<TextBlock Text="{Binding Key}" Margin="5 0 0 0" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
這將創建在右側的標籤,在右側的組合框。 的的ItemsSource爲組合框將來自代碼:
ColorCombo.ItemsSource = ColorsDictionary;
這裏colorsdictionary被定義爲:
Dictionary<string, Color> ColorsDictionary = new Dictionary<string, Color>();
但現在,我試圖通過代碼來添加組合和整個的ItemTemplate。但是我不知道如何通過代碼來實現(綁定數據),任何人都可以幫助我?
歡迎來到SO,請花幾分鐘時間閱讀常見問題解答和Markdown文檔(在編輯問題時可在右側空白處找到有用的synposis)。 – AnthonyWJones 2010-08-03 09:27:03