2016-01-21 46 views
0

我写了这样的事情的ListView的ItemTemplate结合

<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类型的项目)?

编辑:

型 'System.Windows.Markup.XamlParseException' 未处理的异常发生在PresentationFramework.dll

附加: 从Window.Resources

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

和异常申报information:Operacja podawaniawartościelementu“System.Windows.Data.Binding”wywołaławyjątek。,numer wiersza 47,pozycja 46.

顺便说一句我不能得到堆栈跟踪,因为VS在调试时被压坏; __;

好吧,我用System.Diagnostics.PresentationTraceSources.TraceLevel = “高”,并在输出我

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

确实使用了ObservableCollection<Foo>你在XAML中声明了FooTypeToStringConverter? – nkoniishvt

+0

是的,我做到了 – Terrykk

+0

你说你得到一个例外:哪一个?粘贴异常栈跟踪请 – nkoniishvt

回答

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

可以ALSE代替BindableList<Foo>

相关问题