1
我已經動態創建了彈出窗口,該窗口在C#代碼的運行時創建,後面充滿了xaml的內容,並且難以在後面的代碼中綁定它們。當創建它,現在,它遍歷在XAML的項目,併爲每一個相關的複選框:在後臺代碼中綁定動態創建的控件
ListView listView = new ListView();
//Create ListViewItem for each answer
foreach (Answer ans in Questions.DataUsedQuestion.AnswerOptions)
{
ListViewItem item = new ListViewItem();
StackPanel panel = new StackPanel();
CheckBox checkBox = new CheckBox();
TextBlock text = new TextBlock();
panel.Orientation = Orientation.Horizontal;
checkBox.Margin = new Thickness(5, 0, 10, 2);
text.Text = ans.DisplayValue;
panel.Children.Add(checkBox);
panel.Children.Add(text);
item.Content = panel;
listView.Items.Add(item);
}
我也有類似的控制其他地方在在這樣的XAML綁定的應用程序:
<TreeView ItemsSource="{Binding Path=AnswerOptions}" Height="320" Padding="5,5,5,5" Background="Transparent">
<TreeView.ItemTemplate >
<HierarchicalDataTemplate ItemsSource="{Binding Path=AnswerOptions}"
DataType="{x:Type QSB:Answer}" >
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<CheckBox IsChecked="{Binding Path=IsSelected}" >
</CheckBox>
<TextBlock Text="{Binding DisplayValue}" Margin="5,0,0,0" />
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
我怎樣才能做到在後面的代碼類似於上面的東西嗎?
謝謝!這正是我所需要的,除非我實際上並不需要綁定複選框,因爲每個實例都可能有多個實例,並且需要它們獨立運行。 – Saggio 2012-03-07 16:07:02