UserControl.Resources에 정의 된이 사용자 지정 세로 스크롤 막대가 있는데, ItemsControl에 'ItemsSelected'라는 항목이 있습니다.DataGrid 비헤이비어에서 ItemsControl을 바인딩하는 방법
내가 할 일은 DataGridSelectionChanged 동작의 DependencyProperty ItemsControlObject에 바인딩하는 것입니다. 예제 바인딩은 작동하지 않지만 내가 성취하고자하는 것을 보여줍니다. ItemsSelected를 바인딩하기 위해 누락 된 항목은 무엇입니까?
System.Windows.Data 오류 : 4 : 'ElementName = ItemsSelected'참조로 바인딩 소스를 찾을 수 없습니다. BindingExpression : (경로 없음); DataItem = null; 대상 요소는 'DataGridSelectionChanged'(HashCode = 43407976)입니다. target 속성이 같은 것을 구현
<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>
[EDIT]
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);
}
}
바인딩을 통해 당신은 ControlTemplate에 채워 만들 것이다, 당신의 데이터 그리드이 된 ControlTemplate을 사용하는 것이 가정 당신이 당신의 바인딩, 즉'<헬퍼에서'Path'을 지정할 필요가 있다고 생각 : DataGridSelectionChanged ItemsControlObject = "{바인딩 ElementName을 = ItemsSelected , Path =.} "/> ' – XAMlMAX
불행히도 같은 종류의 오류가 발생합니다. System.Windows.Data 오류 : 4 :'ElementName = ItemsSelected '참조로 바인딩 소스를 찾을 수 없습니다. BindingExpression : 경로 = .; DataItem = null; 대상 요소는 'DataGridSelectionChanged'(HashCode = 20692948)입니다. 'ItemsControlObject'(유형 'ItemsControl') – Hank
내가 틀렸다면 나를 정정하되 Binding이'ItemsSource = "대신에'DataContext'를 찾지 못하는 것 같아요 {Binding RelativeSource = {RelativeSource FindAncestor, AncestorType = {x : 유형 UserControl}}, Path = DataContext.MarkerCollection} "''시도'ItemsSource ="{바인딩 관계형 소스 = {RelativeSource FindAncestor, AncestorType = UserControl}, Path = DataContext.MarkerCollection} ". – XAMlMAX