Domanda

Cercando di determinare se è possibile associare il SelectedValue di un ComboBox agli ingressi di molteplici ObjectDataProviders con XAMAL Binding.

Ho guardato MultiBinding, ma che sembra essere il raggruppamento più controlli insieme, non esattamente quello che sto cercando di giorno.

Mi piacerebbe essere in grado di avere la casella combinata (posizioni) cambiare il TextBlock (devianza), che lo fa e di chiamare l'ObjectDataProvider (CommentProvider) per aggiornare la casella di testo (locationComments).

Questo è abbastanza semplice in un code-behind, ma preferisce non seguire questa strada come esperienza di apprendimento.

CODICE XAMAL

<Window.Resources>
    <ObjectDataProvider x:Key="LocationProvider"
        ObjectType="{x:Type srv:ServiceClient}"
        IsAsynchronous="True"MethodName="GetAssignedLocations" />
    <ObjectDataProvider
        x:Key="DevianceProvider"
        ObjectType="{x:Type srv:ServiceClient}"
        IsAsynchronous="True" MethodName="GetPercentChange">
        <ObjectDataProvider.MethodParameters>
            <system:String>Location1</system:String>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <ObjectDataProvider
        x:Key="CommentProvider"
        ObjectType="{x:Type srv:ServiceClient}"
        IsAsynchronous="True"
        MethodName="GetCommentByBusinessUnit">
        <ObjectDataProvider.MethodParameters>
            <system:String>Location1</system:String>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="locations"  VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource LocationProvider}}"
              DisplayMemberPath="BuName" SelectedValuePath="BuKey"
              SelectionChanged="locations_SelectionChanged">
        <ComboBox.SelectedValue>
            <Binding Source="{StaticResource DevianceProvider}"
             Path="MethodParameters[0]"   
                 BindsDirectlyToSource="True" 
                 Mode="OneWayToSource" />
        </ComboBox.SelectedValue>
<TextBlock Name="deviance" Height="23" Margin="0,0,645,17" Width="40" Text="{Binding Source={StaticResource DevianceProvider}}" IsEnabled="False" />

<TextBox Height="23" Margin="0,0,181,17" Name="locationComments" Width="350" />
È stato utile?

Soluzione

Sei sulla strada giusta con il MultiBinding. La chiave è quella di utilizzare un MultiValueCoverter in combinazione con il MultiBinding.

<MultiBinding Converter="{StaticResource Coverter_LocationMultiConverter}"
              Mode="OneWayToSource">
                <Binding Source="{StaticResource DevianceProvider}"
                         Path="MethodParameters[0]"
                         BindsDirectlyToSource="True"
                         Mode="OneWayToSource" />
                <Binding Source="{StaticResource CommentProvider}"
                         Path="MethodParameters[0]"
                         BindsDirectlyToSource="True"
                         Mode="OneWayToSource" />
            </MultiBinding>

Dove Noi stavamo legando ad una sola cosa prima, ora ci sono vincolanti per entrambe le ObjectDataProviders. Il fattore chiave che ci permette di fare questo è il convertitore:

public class LocationMultiCoverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return new object[] { value, value };
    }

    #endregion
}

Perché abbiamo solo bisogno lo stesso valore in entrambi i luoghi del metodo CovertBack è abbastanza semplice, ma sono sicuro che si può vedere che potrebbe essere utilizzato per analizzare alcune cose complesse e passare di nuovo diverse componenti in posti diversi nell'interfaccia utente.

L'utilizzo di questo convertitore possiamo anche provare un piccolo campione, usando due caselle di testo, invece:

<Window x:Class="Sample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Sample"
    Title="Window1"
    Height="300"
    Width="300">
<Window.Resources>
    <local:LocationMultiCoverter x:Key="Coverter_LocationMultiConverter" />
</Window.Resources>
<Grid>
    <StackPanel>
        <TextBlock x:Name="uiDeviance" />
        <TextBlock x:Name="uiComment" />
        <ComboBox x:Name="uiLocations"
                  Height="23"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  SelectedValuePath="Content">
            <ComboBoxItem>1</ComboBoxItem>
            <ComboBoxItem>2</ComboBoxItem>
            <ComboBoxItem>3</ComboBoxItem>
            <ComboBoxItem>4</ComboBoxItem>
            <ComboBoxItem>5</ComboBoxItem>
            <ComboBox.SelectedValue>
                <MultiBinding Converter="{StaticResource Coverter_LocationMultiConverter}"
                              Mode="OneWayToSource">
                    <Binding ElementName="uiDeviance"
                             Path="Text"
                             BindsDirectlyToSource="True" />
                    <Binding ElementName="uiComment"
                             Path="Text"
                             BindsDirectlyToSource="True" />
                </MultiBinding>
            </ComboBox.SelectedValue>
        </ComboBox>
    </StackPanel>
</Grid>

(Il convertitore nel mio esempio esiste nel Codice della finestra alle spalle, come una classe a parte) E come si può vedere testare questo fuori aggiornerà entrambe le caselle di testo quando cambia SelectedValue.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top