【发布时间】:2013-09-18 14:02:57
【问题描述】:
我有一个模型:
public class Table : ViewModelBase
{
private int _id;
private string _north;
private string _east;
private string _south;
private string _west;
public int Id
{
get
{
return _id;
}
set
{
_id = value; OnPropertyChanged();
}
}
public string North
{
get { return _north; }
set { _north = value; OnPropertyChanged();}
}
public string East
{
get { return _east; }
set { _east = value; OnPropertyChanged();}
}
public string South
{
get { return _south; }
set { _south = value; OnPropertyChanged();}
}
public string West
{
get { return _west; }
set { _west = value; OnPropertyChanged();}
}
}
还有声明表列表的 ViewModel:
Tables = new ObservableCollection<Table>();
在 XAML 中:
<ScrollViewer.Resources>
<ItemsPanelTemplate x:Name="MyWrapPanel">
<toolkit:WrapPanel MinWidth="250" Width="{Binding ViewportWidth, ElementName=MyScrollViewer}" />
</ItemsPanelTemplate>
</ScrollViewer.Resources>
<ItemsControl ItemsSource="{Binding Tables}" ItemsPanel="{StaticResource MyWrapPanel}" Grid.RowSpan="2" Grid.Row="1">
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:TableControl
TableNumber="{Binding Id}"
North="{Binding North}"
East="{Binding East}"
South="{Binding South}"
West="{Binding West}"
/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
但是当我将元素添加到表格列表时,一切正常。 但是
TableNumber="{Binding Id}"
North="{Binding North}"
East="{Binding East}"
South="{Binding South}"
West="{Binding West}"
确实没有绑定。
【问题讨论】:
-
什么是
ViewModelBase?您需要在OnPropertyChanged();中指定属性名称。这是通过反射或其他方式在ViewModelBase中发生的吗? -
发布
TableControl的代码和XAML。 -
@PoweredByOrange 是一个feature in .Net 4.5
-
@HighCore 他没有提到他正在使用 .NET 4.5。这些小错误对于 WPF 初学者来说很常见。
-
看起来正确。您的问题可能在其他地方。确保打开数据绑定的调试消息:i.stack.imgur.com/MF8i5.png 接下来,重新运行并检查输出窗口,看看有什么错误。