如何在DataGrid行为中绑定ItemsControl

时间:2014-06-13 05:36:40

标签: wpf xaml wpfdatagrid itemscontrol

我有这个在UserControl.Resources中定义的自定义垂直滚动条,它有一个名为'ItemsSelected'的ItemsControl。

我要做的是将它绑定到行为DataGridSelectionChanged中的DependencyProperty ItemsControlObject。示例绑定不起作用,但显示了我想要实现的内容。绑定ItemsSelected我缺少什么?

System.Windows.Data错误:4:找不到引用'ElementName = ItemsSelected'的绑定源。 BindingExpression :(没有路径); 的DataItem = NULL; target元素是'DataGridSelectionChanged'(HashCode = 43407976); target属性是'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);
    }
}

1 个答案:

答案 0 :(得分: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
}