我有一堆不同的控件(主要是里面有文本块的按钮),我只是放在ItemsControl中 . 在将控件放入ItemsControl之前,所有绑定都正常工作 . 现在,没有任何命令可用,或文本绑定 . 一切只是显示为0 's (I' m绑定到双打) . 我仔细检查以确保我的 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;
}
在我的viewmodel中,我有 ObservableCollection
持有这些 VehicleModel
:
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>
编辑:我正在设置我的datacontext:
<Window.DataContext>
<viewmodel:MainWindowViewModel />
</Window.DataContext>
编辑2:添加了在viewmodel中调用的命令 . 前两个命令参数是模型的属性 .
2 回答
由于ItemsSource的DataContext是Model的集合,对于inners控件,这也是它的DataContext,因此您需要显式指定Path以指向ViewModel属性:
ItemsControl中的每个
<ItemTemplate>
的DataContext
都设置为单个项目 .那么呈现的是什么
您需要更改命令绑定的源代码,以便不是指向导致
VehicleModel.ShowTimeWindowCmd
的默认DataContext.ShowTimeWindowCmd
,而是指向ItemsControl.DataContext.ShowTimeWindowCmd
,它从您的代码中看起来应该会导致MainWindowViewModel.ShowTimeWindowCmd
有很多方法可以做到这一点,但最容易理解的是使用绑定的ElementName属性 .
如果您不想像这样对名称进行硬编码,那么
RelativeSource
绑定也可以在这里使用: