【发布时间】:2016-12-08 13:58:15
【问题描述】:
我有一堆不同的控件(主要是其中带有文本块的按钮),我只是将它们放在 ItemsControl 中。在我将控件放入 ItemsControl 之前,所有绑定都正常工作。现在,没有任何命令起作用,也没有文本绑定。一切都显示为 0(我绑定到双打)。我仔细检查以确保我的ObservableCollection 实际上充满了项目,并且这些项目的属性实际上包含数据。 ItemsControl 确实为集合中的每个项目正确地创建了一个新行。
这是我的模型:
public class VehicleModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private List<double> _nowTime = new List<double>();
public List<double> NowTime
{
get { return _nowTime; }
set { _nowTime = value; OnPropertyChanged("Nowtime"); }
}
private List<double> _VehLat = new List<double>();
public List<double> VehLat
{
get { return _VehLat; }
set { _VehLat = value; OnPropertyChanged("VehLat"); }
}
private int _currentIteration;
public int CurrentIteration //used to hold current index of the list of data fields
{
get { return _currentIteration; }
set
{
_currentIteration = value;
OnPropertyChanged("CurrentIteration");
OnPropertyChanged("CurrentVehLat");
}
}
private double _currentVehLat;
public double CurrentVehLat
{
get { return _currentVehLat; }
set { _currentVehLat = VehLat[CurrentIteration]; OnPropertyChanged("CurrentVehLat"); }
}
}
//Used to loop through the above list and set the currentVehLat equal to
//the current iteration of the list
public void SetData(int i)
{
CurrentIteration = i;
}
在我的视图模型中,我有一个 ObservableCollection 持有这些 VehicleModels:
private ObservableCollection<VehicleModel> _vehicleCollection = new ObservableCollection<VehicleModel>();
public ObservableCollection<VehicleModel> VehicleCollection
{
get
{
return _vehicleCollection;
}
set
{
if (null != value)
{
_vehicleCollection = value;
OnPropertyChanged("VehicleCollection");
}
}
}
private ICommand showTimeWindowCmd;
public ICommand ShowTimeWindowCmd
{
get
{
return showTimeWindowCmd;
}
set
{
showTimeWindowCmd = value;
}
}
public MainWindowViewModel()
{
ShowTimeWindowCmd = new RelayCommand(ShowTimeWindow, param => this.canExecute);
}
public void ShowTimeWindow(object parameter)
{
//do stuff
}
最后,我的 ItemsControl 的 .xaml。我只显示一个,因为它们很多,但它们都完全相同,只是绑定到不同的属性(所有双精度都像视图模型中显示的那样)。注意:控件正确显示,只是绑定不正确:
<ItemsControl Grid.Row="8"
Grid.ColumnSpan="16"
ItemsSource="{Binding VehicleCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
//bunch here
</Grid.ColumnDefinitions>
<Button Grid.ColumnSpan="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding ShowTimeWindowCmd}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="NowTime" />
<Binding Path="VehLat" />
<Binding Source="FISH Latitude" />
<Binding />
</MultiBinding>
</Button.CommandParameter>
<Button.Template>
<ControlTemplate>
<TextBlock FontSize="17"
Text="{Binding Path=CurrentVehLat,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
StringFormat={}{0:F7}}"
Visibility="{Binding IsChecked,
ElementName=FishChkBox,
Converter={StaticResource BoolToVisConverter}}" />
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
编辑:我正在设置我的数据上下文:
<Window.DataContext>
<viewmodel:MainWindowViewModel />
</Window.DataContext>
编辑 2:添加了在视图模型中调用的命令。前两个命令参数是模型的属性。
【问题讨论】:
-
查看输出窗口,您是否看到那里记录了绑定错误?
-
我看到两个错误:一个针对我的“Command={Binding ShowTimeWindowCmd}”,另一个针对布尔属性。两者都在我的视图模型中,而不是我的模型中。它说他们找不到
on object VehicleModel。这两个错误会阻止控件的其余部分绑定到事物吗??
标签: wpf mvvm data-binding