listview itemtemplate绑定

时间:2016-01-21 09:36:23

标签: wpf xaml binding

我写了这样的东西

<ListView Background="{x:Null}" ItemsSource="{Binding Source={StaticResource Foos},Path=FooList}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBox Text="{Binding Path=Name}" VerticalAlignment="Center" HorizontalAlignment="Stretch"/>
                        <ComboBox Grid.Column="1" Background="{x:Null}" VerticalAlignment="Center" HorizontalAlignment="Stretch">
                            <ComboBox.Items>
                                <sys:String>First</sys:String>
                                <sys:String>Second</sys:String>
                                <sys:String>Third</sys:String>
                                <sys:String>Fourth</sys:String>
                            </ComboBox.Items>
                            <ComboBox.SelectedItem>
                                <Binding Converter="{StaticResource FooTypeToStringConverter}"/> <-- this causes an error -->
                            </ComboBox.SelectedItem>
                        </ComboBox>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

Foos

<local:Foos x:Key="Foos"/>

在Foos类的代码后面,FooList是

static BindingList<Foo> fooList = new BindingList<Foo>();
 public static BindingList<Foo> FooList
    {
        get
        {
            return new BindingList<Foo>(fooList.Where //or this is a problem
                (foo =>

                (
                (foo.Name.ToLower()+" "+foo.Number.ToString().ToLower()).Contains(filter)
                || foo.Property2.ToLower().Contains(filter)
                || foo.Property3.ToString().ToLower().Contains(filter)
                || foo.Property4.ToString().ToLower().Contains(filter)
                )

                ).ToList());
        }
        set
        {
            fooList = value;
            OnStaticPropertyChanged("FooList");
        }
    }

此时,转换器FooTypeToStringConverter如下所示:

public class FooTypeToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        //Dispatcher.CurrentDispatcher.BeginInvoke((Action)(() => { MessageBox.Show(value.GetType().ToString()); }));
        return Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

所以这不是IMO的原因。

我觉得我做了些蠢事,但我不知道 - 什么。你能帮帮我吗?

<Binding Converter="{StaticResource FooTypeToStringConverter}"/> <-- this causes an error -->

我不应该收到FooList项(Foo类型的项目)吗?

编辑: 来自Window.Resources的声明

<FooControl.Resources>
<local:FooTypeToStringConverter x:Key="FooTypeToStringConverter"/>
</FooControl.Resources>

和例外:

PresentationFramework.dll中出现未处理的“System.Windows.Markup.XamlParseException”类型异常

其他信息:Operacjapodawaniawartościlementu“System.Windows.Data.Binding”wywołaławyjątek。,numer wiersza 47,pozycja 46。

BTW我无法获得堆栈跟踪,因为VS在调试时会被压缩; __;

好的,我使用了System.Diagnostics.PresentationTraceSources.TraceLevel =“High”,在输出中我得到了

System.Windows.Data Warning: 56 : Created BindingExpression (hash=14506096) for Binding (hash=28492826)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=14506096): Default mode resolved to TwoWay
System.Windows.Data Warning: 61 : BindingExpression (hash=14506096): Default update trigger resolved to PropertyChanged

1 个答案:

答案 0 :(得分:1)

根据您发布的警告 System.Windows.Data Warning: 58 : Path: '' Path属性设置为null。这意味着你必须设置它。通常你可以这样做:

SelectedItem="{Binding}"

也与

相同
SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}"/>

并使用转换器:

SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}, Converter="{StaticResource FooTypeToStringConverter}"

您也可以使用ObservableCollection<Foo>代替BindableList<Foo>