2

MSDN から次のサンプルがあります。

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="25" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <riaControls:DomainDataSource Name="source" QueryName="GetProductsByColor" AutoLoad="true">
        <riaControls:DomainDataSource.DomainContext>
            <domain:ProductDomainContext />
        </riaControls:DomainDataSource.DomainContext>
        <riaControls:DomainDataSource.QueryParameters>
            <riaControls:Parameter ParameterName="color" Value="{Binding ElementName=colorCombo, Path=SelectedItem.Content}" />
        </riaControls:DomainDataSource.QueryParameters>
    </riaControls:DomainDataSource>
    <ComboBox Width="60"  Grid.Row="0" x:Name="colorCombo">
        <ComboBoxItem Content="Black" />
        <ComboBoxItem Content="Blue" />
    </ComboBox>
    <data:DataGrid Grid.Row="1" ItemsSource="{Binding Data, ElementName=source}" />
</Grid>

ご覧のとおり、パラメータ バインディングを作成する行が設定されています。

<riaControls:Parameter ParameterName="color" Value="{Binding ElementName=colorCombo, Path=SelectedItem.Content}" /> 

ただし、コードでこれを試してもうまくいきません:

Binding binding = new Binding();
binding.ElementName = "colorCombo";
//binding.Source = this.colorCombo; also doesn't work!
binding.Path = new PropertyPath("SelectedItem.Content");
binding.Mode = BindingMode.OneWay;

Parameter param = new Parameter();
param.Value = binding;

source.QueryParameters.Clear();
source.QueryParameters.Add(param);

私が間違っていることについてのアイデアはありますか?

4

1 に答える 1

2

プログラムでバインドするには、を使用する必要がありますBindingOperations.SetBinding。現在、ValueプロパティをBindingバインドするのではなく、インスタンスに設定しているだけです。

BindingOperations.SetBinding(param, Parameter.ValueProperty, binding)

さらに、バインディングを に戻しますSource = this.colorCombo。要素名は通常、分離コードには存在しない XAML コンテキスト内で解決されます。

于 2011-07-25T21:24:55.750 に答える