我有一堆不同的控件(主要是帶有textblocks的按鈕),我只是放在一個ItemsControl中。在將控件放入ItemsControl之前,所有綁定都能正常工作。現在,沒有任何命令可用,或者文本綁定。一切都顯示爲0(我綁定到雙打)。我仔細檢查以確保我的ObservableCollection
實際上裝滿了物品,並且這些物品的屬性實際上有數據。 ItemsControl確實爲集合中的每個項目創建了一個新行。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
持有這些VehicleModel
S:
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:添加了命令被稱爲在視圖模型。前兩個命令參數是模型的屬性。
查找到輸出窗口,你看到綁定錯誤記錄嗎? –
我看到兩個錯誤:一個用於我的'Command = {Binding ShowTimeWindowCmd}',另一個用於布爾屬性。這兩個都在我的視圖模型中,而不是我的模型。它說他們沒有被發現在「對象VehicleModel」上。這兩個錯誤會阻止其他控件綁定到事物嗎? – pfinferno