列表视图项模板绑定
listview itemtemplate 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>
福斯在哪里
<local:Foos x:Key="Foos"/>
以及来自 class 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>
和异常:
'System.Windows.Markup.XamlParseException' 类型的未处理异常发生在 PresentationFramework.dll
其他信息:Operacja podawania wartości elementu „System.Windows.Data.Binding” wywołała wyjątek., numer wiersza 47, pozycja 46.
顺便说一句,我无法获得堆栈跟踪,因为调试时 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
根据您发布的警告
System.Windows.Data Warning: 58 : Path: ''
Path
属性 设置为空。这意味着你必须设置它。通常你可以这样做:
SelectedItem="{Binding}"
与
的含义相同
SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}"/>
并带有转换器:
SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}, Converter="{StaticResource FooTypeToStringConverter}"
您也可以使用 ObservableCollection<Foo>
代替 BindableList<Foo>
我写了这样的东西
<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>
福斯在哪里
<local:Foos x:Key="Foos"/>
以及来自 class 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>
和异常:
'System.Windows.Markup.XamlParseException' 类型的未处理异常发生在 PresentationFramework.dll
其他信息:Operacja podawania wartości elementu „System.Windows.Data.Binding” wywołała wyjątek., numer wiersza 47, pozycja 46.
顺便说一句,我无法获得堆栈跟踪,因为调试时 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
根据您发布的警告
System.Windows.Data Warning: 58 : Path: ''
Path
属性 设置为空。这意味着你必须设置它。通常你可以这样做:
SelectedItem="{Binding}"
与
的含义相同SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}"/>
并带有转换器:
SelectedItem = "{Binding DataContext,RelativeSource={RelativeSource Self}}, Converter="{StaticResource FooTypeToStringConverter}"
您也可以使用 ObservableCollection<Foo>
代替 BindableList<Foo>