2009-05-08 26 views
6

Ho un UserControl con ComboBox che, sulla base di dati XML:WPF: Associazione a ComboBox SelectedItem

<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> 

Ecco l'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 [email protected]}" Grid.Column="0"> 
       <Image Source="{Binding [email protected]}" 
         Width="16" 
         Height="16" 
         Margin="3" /> 
      </Border> 
      <Border Background="{Binding [email protected]}" Grid.Column="1"> 
       <TextBlock Foreground="{Binding [email protected]}" 
          Margin="3" 
          Text="{Binding [email protected]}" /> 
      </Border> 
      <Border Background="{Binding [email protected]}" Grid.Column="2"> 
       <TextBlock Foreground="{Binding [email protected]}" 
          Margin="3" 
          FontWeight="Bold" 
          Text="{Binding [email protected]}" /> 
      </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> 

Nel MainForm.xaml ho un TextBox che voglio associare al mio UserControl SelectedItem.

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

Sarò felice se mi guiderai come farlo.

Grazie in anticipo!

risposta

12

Il trucco qui è che quando si deve associare a SelectedItem su un ItemControl associato a XML, l'elemento selezionato stesso è un XmlElement e si deve usare XPath per ottenere l'elemento/attributo necessario.

Il modo più semplice per raggiungere questo obiettivo è quello di utilizzare DataContext:

<TextBox x:Name=textBoxST 
    DataContext="{Binding ElementName=comboBoxST, Path=SelectedItem}" 
    Text="{Binding [email protected]}"/> 
+0

Ciao saldoukhov! Grazie per la risposta, ma, sfortunatamente, la tua soluzione non funziona :-(. Forse a causa del legame XML del ComboBox originale incapsulato in UserControl? – user83493

+0

Ha funzionato per me in Silverlight 5 senza dover specificare 'XPath', semplicemente legando al nome della proprietà dell'oggetto di destinazione come 'Text = {Binding Description}'. – OmegaMan

1

La risposta postato sopra è stato per il caso di una casella di riepilogo posto direttamente sul modulo. Nel caso di UserControl e di ComboBox basato su modelli, eviterei il binding di xml puro: troppi fattori possono infrangerlo. Invece, utilizzare questo codice per creare una proprietà di dipendenza:

public MyComboBox() 
    { 
     InitializeComponent(); 
     myComboBox.SelectionChanged += MyComboBoxSelectionChanged; 
    } 

    void MyComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     SetValue(SelValueProperty, ((XmlElement)e.AddedItems[0]).Attributes["Value"].Value); 
    } 

    public static readonly DependencyProperty SelValueProperty = 
     DependencyProperty.Register("SelValue", typeof(string), typeof(MyComboBox), 
      new FrameworkPropertyMetadata("")); 

E vincolante è semplice, allora:

<TextBox x:Name=textBoxST Text="{Binding ElementName=comboBoxST, Path=SelValue}"/> 
2

Per quello che vale, ho preferito precedente approccio di Sergey meglio. Tuttavia, nel mio scenario, ho avuto un'etichetta invece di una casella di testo, ma questo ha funzionato per me:

<Label x:Name="labelST" Content="{Binding ElementName=comboBoxST, Path=SelectedValue}"/> 

Spaciba, Sergey.