ListViewItems的多重绑定问题

时间:2011-05-09 09:32:08

标签: c# .net wpf wpf-controls binding

我有这片XAML:

<Style x:Key="HideShowStyle" TargetType="{x:Type ListViewItem}">
        <Style.Resources>
            <localConverters:ShowHideConverter x:Key="showHideConverter" />
        </Style.Resources>

        <Style.Triggers>
            <DataTrigger Value="true">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource showHideConverter}">
                        <Binding Path="EndingDate" />
                        <Binding ElementName="cmbType" Path="SelectedValue" />
                        <Binding ElementName="searchBox" Path="Text" />
                        <Binding Path="Client" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Visibility" Value="Visible" />
            </DataTrigger>

            <DataTrigger Value="false">
                (...)
            </DataTrigger>
        </Style.Triggers>
    </Style>

我已在C#代码中转换为:

        var triggerStyle = new Style();
        var trueTrigger = new DataTrigger();
        var multiBinding = new MultiBinding();
        var converter = new ShowHideConverter();

        multiBinding.Converter = converter;

        var binding1 = new Binding("SelectedItem");
        binding1.Source = cmbType;
        multiBinding.Bindings.Add(binding1);

        var binding2 = new Binding("EndingDate");
        binding2.Source = reportList.ItemsSource;
        multiBinding.Bindings.Add(binding2);

        var binding3 = new Binding("Text");
        binding3.Source = searchBox;
        multiBinding.Bindings.Add(binding3);

        var binding4 = new Binding("Client");
        binding4.Source = reportList.ItemsSource;
        multiBinding.Bindings.Add(binding4);

        trueTrigger.Value = true;
        trueTrigger.Binding = multiBinding;
        trueTrigger.Setters.Add(new Setter(VisibilityProperty, Visibility.Visible));

        var falseTrigger = new DataTrigger();
        falseTrigger.Value = false;
        falseTrigger.Binding = multiBinding;
        falseTrigger.Setters.Add(new Setter(VisibilityProperty, Visibility.Collapsed));

        triggerStyle.Triggers.Add(trueTrigger);
        triggerStyle.Triggers.Add(falseTrigger);

        reportList.ItemContainerStyle = triggerStyle;

现在,执行代码后,listview将数据正确传递到转换器中,对其进行分析并根据条件返回true或false。问题是,“客户端”绑定不会滚动行,但它仍然停留在第一列的值上,而XAML硬编码样式工作正常。
我不知道如何使其有效。

1 个答案:

答案 0 :(得分:2)

只需删除此行:

binding4.Source = reportList.ItemsSource;

绑定的来源将是列表视图项的数据上下文(这是每行的数据项)。