我有一堆不同的控件(主要是里面有文本块的按钮),我只是放在ItemsControl中。在将控件放入ItemsControl之前,所有绑定都正常工作。现在,没有任何命令可用,或文本绑定。一切都只是显示为0(我与双打绑定)。我仔细检查以确保我的whcar_t* source,destiantion;
int location;
copy(source,destination,location,partOfSource);
实际上已经填充了商品,而且那些商品'属性实际上有数据。 ItemsControl为集合中的每个项目正确创建了一个新行。
这是我的模特:
ObservableCollection
在我的视图模型中,我有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
s:
VehicleModel
最后,我的ItemsControl的.xaml。我只展示一个,因为它们中有很多,但它们都完全相同,只是绑定到不同的属性(所有双打都像在视图模型中显示的那样)。注意:控件正确显示,而不是绑定:
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
}
编辑:我正在设置我的datacontext:
<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>
编辑2:添加了在viewmodel中调用的命令。前两个命令参数是模型的属性。
答案 0 :(得分:1)
由于ItemsSource的DataContext是Model的集合,对于inners控件,这也是它的DataContext,因此您需要显式指定指向ViewModel属性的Path:
<Button Grid.ColumnSpan="4"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}},
Path=DataContext.ShowTimeWindowCmd}"
>
答案 1 :(得分:1)
ItemsControl中每个DataContext
的{{1}}都设置为单个项目。
所呈现的是
<ItemTemplate>
您需要更改命令绑定的源代码,以便不是指向导致<ItemsControl ItemsSource="{Binding VehicleCollection}">
<ContentPresenter> <!-- DataContext is VehicleModel[0] -->
<Grid...>
<!-- DataContext is inherited, so still VehicleModel[0] -->
<Button Command="{Binding ShowTimeWindowCmd}" .. />
...
</Grid>
</ContentPresenter>
<ContentPresenter> <!-- DataContext is VehicleModel[1] -->
<Grid...>
<!-- DataContext is inherited, so still VehicleModel[1] -->
<Button Command="{Binding ShowTimeWindowCmd}" .. />
...
</Grid>
</ContentPresenter>
etc...
</ItemsControl>
的默认DataContext.ShowTimeWindowCmd
,而是指向VehicleModel.ShowTimeWindowCmd
,它从您的代码中看起来像它一样应该导致ItemsControl.DataContext.ShowTimeWindowCmd
有很多方法可以做到这一点,但最容易理解的是使用绑定的ElementName属性。
MainWindowViewModel.ShowTimeWindowCmd
如果您不想像这样对名称进行硬编码,<ItemsControl x:Name="MyItemsControl"...>
...
<Button Command="{Binding ElementName=MyItemsControl, Path=DataContext.ShowTimeWindowCmd}" .. />
...
</ItemsControl>
绑定也可以在这里使用:
RelativeSource