هل من الممكن ربط WPF Combobox.SelectedValue إلى ObjectDataProviders متعددة؟

StackOverflow https://stackoverflow.com/questions/848946

سؤال

وفي محاولة لتحديد ما إذا كان من الممكن ربط SelectedValue من تحرير وسرد لمدخلات ObjectDataProviders متعددة مع XAMAL الربط.

ونظرت MultiBinding لكن الذي يظهر أن تضم عناصر تحكم متعددة معا، وليس بالضبط ما كنت أبحث بعد يوم.

وأود أن تكون قادرة على تحرير وسرد (المواقع) تغيير TextBlock (الانحراف) وهو ما يحدث بالفعل واستدعاء ObjectDataProvider (CommentProvider) لتحديث مربع نص (locationComments).

وهذا واضح وصريح إلى حد ما في التعليمات البرمجية الخلفية ولكن يفضل أن لا يذهب هذا الطريق بأنها تجربة التعلم.

وXAMAL CODE

<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" />
هل كانت مفيدة؟

المحلول

وأنت على الطريق الصحيح مع MultiBinding. والمفتاح هو لاستخدام MultiValueCoverter بالتزامن مع 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>

وأين كنا ملزمة لشيء واحد فقط قبل، ونحن الآن ملزمة لكلا ObjectDataProviders. العامل الرئيسي الذي يتيح لنا القيام بذلك هو تحويل:

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
}

ولأن نحن فقط بحاجة إلى نفس القيمة في كل من الأماكن طريقة CovertBack بسيط للغاية، ولكن أنا متأكد من أنك يمكن أن نرى أنه يمكن استخدامها لتحليل بعض الاشياء المعقدة وتمرير الظهر مكونات مختلفة لأماكن مختلفة في واجهة المستخدم.

<ع> استخدام هذا المحول يمكننا أيضا تجربة عينة صغيرة، وذلك باستخدام مربعي نص بدلا من ذلك:

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

و(محول في بلدي على سبيل المثال موجود في التعليمات البرمجية نافذة وراء كطبقة منفصلة) وكما ترون اختبار هذا الخروج فإنه سيتم تحديث كل من مربعات النص عندما يتغير SelectedValue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top