Всем привет. Нужна ваша помощь Имеем ListBox, в котором используются ItemTemplate и привязка к xml. Не могу понять как получать доступ (в codbehind) к элементам, содержащимся в ListBoxItem Для понятности набросал пример: Разметка xaml:
| Код | <Window x:Class="QuestionForForum.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="0" d:DesignWidth="0" SizeToContent="WidthAndHeight"> <Grid> <ListBox Name="Lbox"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <TextBlock Text="{Binding Path=Attribute[Text1].Value}" Grid.Row="0" Margin="2" FontWeight="DemiBold" TextInput="TextBlock_TextInput"></TextBlock> <TextBlock Text="{Binding Path=Attribute[Text2].Value}" Grid.Row="1" Margin="2"></TextBlock> <ComboBox Name="CbTemp" Grid.Row="2"> <ComboBoxItem>Value1</ComboBoxItem> <ComboBoxItem>Value2</ComboBoxItem> <ComboBoxItem>Value3</ComboBoxItem> </ComboBox> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window>
|
Файл xml к которому "биндимся":
| Код | <?xml version="1.0" encoding="utf-8" ?> <Temp> <row Text1="Value1" Text2="Bla-bla-bla"></row> <row Text1="Value1" Text2="Bla-bla-bla"></row> <row Text1="Value2" Text2="Bla-bla-bla"></row> <row Text1="Value3" Text2="Bla-bla-bla"></row> <row Text1="Value3" Text2="Bla-bla-bla"></row> </Temp>
|
CodeBehind:
| Код | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Resources; using System.Xml.Linq; namespace QuestionForForum { /// <summary> /// Логика взаимодействия для MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); XElement doc = XElement.Parse(Properties.Resources.XMLFile1); this.Lbox.ItemsSource = doc.Elements("row"); } private void TextBlock_TextInput(object sender, TextCompositionEventArgs e) { } } }
|
Вот допусти в этом примере надо сделать так, чтобы в ComboBox (Name="CbTemp") выбирался ComboBoxItem в зависимости от текста в первом TextBlock. Логика подсказывает, что надо через TextInput, но как получить доступ к ComboBox в душе не знаю. Находил пример использования ItemContainerGenerator.ContainerFromIndex() (или ItemContainerGenerator.ContainerFromItem()), но так и не смог "привязать" его в этот пример. Еще раз уточню, на всякий случай - использовать только CodeBehind |