2014-06-13 97 views
0

我有這個自定義垂直滾動條是在UserControl.Resources中定義的,它有一個ItemsControl,名爲'ItemsSelected'。如何在DataGrid行爲中綁定ItemsControl

我想要做的是將其綁定到行爲DataGridSelectionChanged中的DependencyProperty ItemsControlObject。示例綁定不起作用,但顯示了我想要實現的內容。我缺少什麼來綁定ItemsSelected?

System.Windows.Data錯誤:4:找不到與參考'ElementName = ItemsSelected'綁定的源。 BindingExpression:(no path); DataItem = null; 目標元素是'DataGridSelectionChanged'(HashCode = 43407976); 目標屬性是 'ItemsControlObject'(類型 '的ItemsControl')

<UserControl> 
    <UserControl.Resources> 
     <ControlTemplate x:Key="VerticalScrollBar" TargetType="{x:Type ScrollBar}"> 
      <Grid> 
       ... 
       <!-- BEGIN --> 
       <ItemsControl Name="ItemsSelected" VerticalAlignment="Stretch" 
           ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.MarkerCollection}"> 
        <ItemsControl.ItemTemplate> 
         <DataTemplate> 
          <Rectangle Fill="Gray" Width="18" Height="4"/> 
         </DataTemplate> 
        </ItemsControl.ItemTemplate> 
        <ItemsControl.ItemContainerStyle> 
         <Style TargetType="ContentPresenter"> 
          <Setter Property="Canvas.Top" Value="{Binding}" /> 
         </Style> 
        </ItemsControl.ItemContainerStyle> 
        <ItemsControl.ItemsPanel> 
         <ItemsPanelTemplate> 
          <Canvas/> 
         </ItemsPanelTemplate> 
        </ItemsControl.ItemsPanel> 
       </ItemsControl> 
       <!-- END --> 
      </Grid> 
     </ControlTemplate> 

     <Style TargetType="{x:Type DataGrid}" > 
      <Style.Resources> 
       <Style x:Key="{x:Type ScrollBar}" TargetType="{x:Type ScrollBar}"> 
        <Setter Property="SnapsToDevicePixels" Value="True" /> 
        <Setter Property="OverridesDefaultStyle" Value="true" /> 
        <Style.Triggers> 
         <Trigger Property="Orientation" Value="Horizontal"> 
          <Setter Property="Width" Value="Auto" /> 
          <Setter Property="Height" Value="18" /> 
          <Setter Property="Template" Value="{StaticResource HorizontalScrollBar}" /> 
         </Trigger> 
         <Trigger Property="Orientation" Value="Vertical"> 
          <Setter Property="Width" Value="18" /> 
          <Setter Property="Height" Value="Auto" /> 
          <Setter Property="Template" Value="{StaticResource VerticalScrollBar}" /> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Style.Resources> 
     </Style> 
    </UserControl.Resources> 

    <Grid Name="gridUsers" Background="Transparent"> 
     <DockPanel> 
      <DataGrid Name="GenericDataGrid"> 
       <i:Interaction.Behaviors> 
        <helpers:DataGridSelectionChanged ItemsControlObject="{Binding ElementName=ItemsSelected}" /> 
       </i:Interaction.Behaviors> 
       <DataGrid.Columns> 
        ... 
       </DataGrid.Columns> 
      </DataGrid> 
     </DockPanel> 
    </Grid> 
</UserControl> 

[編輯]

public static class ScrollBarMarkers 
{ 
    public static readonly DependencyProperty MarkersSelectedCollectionProperty = 
     DependencyProperty.RegisterAttached("MarkersSelectedCollection", typeof(ObservableCollection<double>), typeof(ScrollBarMarkers), new PropertyMetadata(null)); 

    public static ObservableCollection<double> GetMarkersSelectedCollection(DependencyObject obj) 
    { 
     return (ObservableCollection<double>)obj.GetValue(MarkersSelectedCollectionProperty); 
    } 

    public static void SetMarkersSelectedCollection(ItemsControl obj, ObservableCollection<double> value) 
    { 
     obj.SetValue(MarkersSelectedCollectionProperty, value); 
    } 
} 
+0

假設你在DataGrid中使用該控件模板,這將使控件模板填充通過有約束力的,我認爲你需要在你的綁定即'<傭工指定'Path':DataGridSelectionChanged ItemsControlObject =「{綁定的ElementName = ItemsSelected ,Path =。}「/>' – XAMlMAX

+0

不幸的是,同樣的錯誤發生:System.Windows.Data錯誤:4:找不到與參考'ElementName = ItemsSelected'綁定的源。 BindingExpression:路徑= .;的DataItem = NULL;目標元素是'DataGridSelectionChanged'(HashCode = 20692948);目標屬性是'ItemsControlObject'(類型'ItemsControl') – Hank

+1

糾正我,如果我錯了,但我認爲綁定失敗找到實際的'DataContext',而不是'ItemsSource = {Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type UserControl}},Path = DataContext.MarkerCollection}「'try'ItemsSource =」{Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = UserControl},Path = DataContext.MarkerCollection}「'。 – XAMlMAX

回答

1

實施這樣的事情停止需要結合實際的ItemsControl

這裏被結合:

<ItemsControl ItemsSource="{Binding Source={x:Static helpers:MyClass.Instance}, Path=SelectedMarkers}"> 

這裏是單身模式的類

public class MyClass : INotifyPropertyChanged 
{ 
    public static ObservableCollection<double> m_selectedMarkers = new ObservableCollection<double>(); 
    public ObservableCollection<double> SelectedMarkers 
    { 
     get 
     { 
      return m_selectedMarkers; 
     } 
     set 
     { 
      m_selectedMarkers = value; 
      NotifyPropertyChanged(); 
     } 
    } 


    private static MyClass m_Instance; 
    public static MyClass Instance 
    { 
     get 
     { 
      if (m_Instance == null) 
      { 
       m_Instance = new MyClass(); 
      } 

      return m_Instance; 
     } 
    } 

    private MyClass() 
    { 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion 
} 
相關問題