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);
}
}
假設你在DataGrid中使用該控件模板,這將使控件模板填充通過有約束力的,我認爲你需要在你的綁定即'<傭工指定'Path':DataGridSelectionChanged ItemsControlObject =「{綁定的ElementName = ItemsSelected ,Path =。}「/>' – XAMlMAX
不幸的是,同樣的錯誤發生:System.Windows.Data錯誤:4:找不到與參考'ElementName = ItemsSelected'綁定的源。 BindingExpression:路徑= .;的DataItem = NULL;目標元素是'DataGridSelectionChanged'(HashCode = 20692948);目標屬性是'ItemsControlObject'(類型'ItemsControl') – Hank
糾正我,如果我錯了,但我認爲綁定失敗找到實際的'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