lundi 31 août 2015

XAML Binding: Combobox populating textbox in one scenario, but not another? WPF

I have one set of code working properly. It loads the "address" field from a database of user records containing stuff like [ID, Fname, Lname, address, zip, etc] into a ComboBox's selection set. Once a user selects an address it displays that selection in a corresponding textbox as well.

working code:

<Window x:Class="CC_Ticketing_WPF.MainWindow"
       xmlns:local="clr-namespace:CC_Ticketing_WPF"
       Title="MainWindow">
    <Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
            <StackPanel>
                <TextBlock Text="{Binding XPath=address}"/>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ComboBox x:Name="cb_Address" 
                  DataContext="{StaticResource orderheaderDataSource}" 
                  ItemsSource="{Binding XPath=/dataroot/orderheader/address}"  
                  IsEditable="True"/>
        <TextBlock x:Name="tb_Address" 
                   Text="{Binding ElementName=cb_Address,Path=Text}"/>
    </StackPanel>
</Window>

The problem is I know that's very limited as it relies on defining everything in the stack panel. I am trying this something along these lines instead:

broken code

<Window x:Class="CC_Ticketing_WPF.MainWindow"
        xmlns:local="clr-namespace:CC_Ticketing_WPF"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow">
    <Window.Resources>
        <DataTemplate x:Key="orderheaderTemplate">
        </DataTemplate>
    </Window.Resources>
    <StackPanel>
        <ComboBox x:Name="cb_Address" 
                  DataContext="{StaticResource orderheaderDataSource}" 
                  ItemsSource="{Binding XPath=/dataroot/orderheader}" 
                  DisplayMemberPath="address" 
                  SelectedValuePath="Content" 
                  IsEditable="True"/>
        <TextBlock x:Name="tb_Address" 
                   Text="{Binding ElementName=cb_Address,
                                  Path=SelectedItem.Content,
                                  Mode=OneWay}"/>
    </StackPanel>
</Window>

This loads the addresses and allows selection in the combobox, but sends nothing to the textbox. Eventually I'd want selecting an address from this combox to not only send the selection to the textbox, but also send the other relevant data from that record to other textbox's (like you select just an address and it populates a bunch of textboxes with the address, name, zip, etc.) Pretty common business need. Could someone put me on the right track here to accomplish this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire