2016-12-08 40 views
1

我有一堆不同的控件(主要是帶有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:添加了命令被稱爲在視圖模型。前兩個命令參數是模型的屬性。

+0

查找到輸出窗口,你看到綁定錯誤記錄嗎? –

+0

我看到兩個錯誤:一個用於我的'Command = {Binding ShowTimeWindowCmd}',另一個用於布爾屬性。這兩個都在我的視圖模型中,而不是我的模型。它說他們沒有被發現在「對象VehicleModel」上。這兩個錯誤會阻止其他控件綁定到事物嗎? – pfinferno

回答

1

至於在DataContext的的ItemsSource是模式的集合,對於內部件控制,這將是它的DataContext的,所以你需要明確指定的路徑指向視圖模型屬性:

<Button Grid.ColumnSpan="4" 
    HorizontalAlignment="Center" 
    VerticalAlignment="Center" 
    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, 
    Path=DataContext.ShowTimeWindowCmd}" 
    > 
1

DataContext您的ItemsControl中的每個<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看起來從您的代碼像它應該導致MainWindowViewModel.ShowTimeWindowCmd

有很多方法可以做到這一點,但最容易理解的是通過使用綁定的ElementName屬性。

<ItemsControl x:Name="MyItemsControl"...> 
    ... 
    <Button Command="{Binding ElementName=MyItemsControl, Path=DataContext.ShowTimeWindowCmd}" .. /> 
    ... 
</ItemsControl> 

一個RelativeSource結合也將在這裏工作,如果你不想硬編碼像這樣的名稱:

<ItemsControl ...> 
    ... 
    <Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.ShowTimeWindowCmd}" .. /> 
    ... 
</ItemsControl> 
+0

很好的解釋。我沒有意識到ItemsControl中的每個項目都設置了datacontext。現在有道理!謝謝。 – pfinferno