WPF: Binding zu SelectedItem ComboBox

Ich habe ein UserControl mit ComboBox, die basierend auf XML-Daten:

<Root>
<Node Background="Yellow" Foreground="Cyan" Image="1.ico" Property="aaaa" Value="28" />
<Node Background="SlateBlue" Foreground="Black" Image="2.ico" Property="bbbb" Value="2.5" />
<Node Background="Teal" Foreground="Green" Image="3.ico" Property="cccc" Value="4.0" />
<Node Background="Yellow" Foreground="Red" Image="4.ico" Property="dddd" Value="0" /></Root>

Hier ist die UserControl-XAML:

<UserControl x:Class="xxxxxxxx.MyComboBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         x:Name="myComboBoxControl">
<UserControl.Resources>
    <DataTemplate x:Key="dataTemplateNode">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto" MinWidth="20"/>
            </Grid.ColumnDefinitions>
            <Border Background="{Binding XPath=@Background}" Grid.Column="0">
                <Image Source="{Binding XPath=@Image}" 
                       Width="16" 
                       Height="16" 
                       Margin="3" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="1">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3"
                           Text="{Binding XPath=@Property}" />
            </Border>
            <Border Background="{Binding XPath=@Background}" Grid.Column="2">
                <TextBlock Foreground="{Binding XPath=@Foreground}" 
                           Margin="3" 
                           FontWeight="Bold"
                           Text="{Binding XPath=@Value}" />
            </Border>
        </Grid>
    </DataTemplate>

    <XmlDataProvider x:Key="xmlNodeList" 
                     Source="/data/Combo.xml" 
                     XPath="/Root/Node"/>
</UserControl.Resources>

<ComboBox Name="myComboBox" 
          ItemsSource="{Binding Source={StaticResource xmlNodeList}}" 
          ItemTemplate="{StaticResource dataTemplateNode}"
          HorizontalContentAlignment="Stretch" /></UserControl>

In der MainForm.xaml-ich habe eine TextBox, möchte ich zu binden, das mein UserControl SelectedItem.

<StackPanel Orientation="Horizontal">
<local:MyComboBox1 x:Name="comboBoxST" />
<TextBox x:Name="textBoxST"/></StackPanel>

Werde ich froh sein, wenn Sie guid mir, wie zu tun.

Vielen Dank im Voraus!

InformationsquelleAutor user83493 | 2009-05-08

Schreibe einen Kommentar