문제

Combobox의 선택된 값을 Xamal 바인딩을 갖는 여러 ObjectDataproviders의 입력에 바인딩 할 수 있는지 확인하려고합니다.

나는 멀티 핀딩을 보았지만 그것은 내가보고있는 것이 아니라 여러 컨트롤을 함께 그룹화하는 것처럼 보입니다.

Combobox (위치)가 수행하는 TextBlock (Deviance)을 변경하고 ObjectDataprovider (CommentProvider)를 호출하여 TextBox (LocationComments)를 업데이트 할 수 있기를 원합니다.

이것은 코드-홀드에서 상당히 간단하지만이 경로를 학습 경험으로 가지 않는 것을 선호합니다.

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" />
도움이 되었습니까?

해결책

당신은 멀티 바인딩으로 올바른 길을 가고 있습니다. 핵심은 멀티 핀딩과 함께 다중 큐버터를 사용하는 것입니다.

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

우리가 전에 한 가지에 구속력이 있었던 곳에서, 이제 우리는 그것을 대상 dataproviders에 모두 묶고 있습니다. 이를 수행 할 수있는 핵심 요소는 변환기입니다.

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
}

우리는 두 곳 모두에서 동일한 값이 필요하기 때문에 표지 방법은 매우 간단하지만 복잡한 물건을 구문 분석하고 UI의 다른 구성 요소를 다른 장소로 전달하는 데 사용될 수 있음을 알 수 있습니다.

이 변환기를 사용하여 대신 두 개의 텍스트 상자를 사용하여 작은 샘플을 시도 할 수도 있습니다.

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

(내 예제의 변환기는 별도의 클래스로 창의 코드에 존재합니다)이를 테스트 할 수 있듯이 선택한 값이 변경 될 때 두 텍스트 상자를 업데이트합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top