如何为ItemsControl创建附加属性

时间:2014-06-12 23:55:00

标签: c# wpf xaml itemscontrol attached-properties

我有以下ItemsControl,如图所示它具有硬编码值,我想将这些值转换为附加属性,可能是ObservableCollection或类似的东西。

如何创建此附加属性以及如何绑定它。

<ItemsControl Grid.Column="0" VerticalAlignment="Stretch" Name="ItemsSelected">
    <sys:Double>30</sys:Double>
    <sys:Double>70</sys:Double>
    <sys:Double>120</sys:Double>
    <sys:Double>170</sys:Double>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Rectangle Fill="SlateGray" 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>

[编辑] 所以我认为我有附属物:

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);
    }
}

我现在想知道的是在选择更改行为中调用以下内容之前获取ItemsControl对象的最佳方法:

ScrollBarMarkers.SetMarkersSelectedCollection(ItemsControl, initSelected);

自定义垂直滚动条的样式在Window.Resources

中设置

行为在DataGrid上设置如下:

<DataGrid Name="GenericDataGrid">
    <i:Interaction.Behaviors>
        <helpers:DataGridSelectionChanged />
    </i:Interaction.Behaviors>
</DataGrid>

我的选择改变了行为:

public class DataGridSelectionChanged : Behavior<DataGrid>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        this.AssociatedObject.SelectionChanged += DataGrid_SelectionChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        this.AssociatedObject.SelectionChanged -= DataGrid_SelectionChanged;
    }

    void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ObservableCollection<double> initSelected = new ObservableCollection<double>();
        initSelected.Add(30);
        initSelected.Add(60);
        initSelected.Add(100);
        //Just trying to figure out how best to get the ItemsControl object.
        ScrollBarMarkers.SetMarkersSelectedCollection(itemsControlObj, initSelected);
    }
}

下面是滚动条中标记的示例,ItemsControl已根据问题顶部的代码添加到自定义垂直滚动条。 enter image description here

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你想把一个ObservableCollection绑定到ItemsControl,当项目很长时,会出现滚动条。

此解决方案可以为您服务。

[我将使用MVVM]

您可以在代码中创建ObservableCollection。

 private ObservableCollection<int> _list = new ObservableCollection<int>();
 public ObservableCollection<int> List
    {
        get { return _list ; }
        set { _list = value; RaisePropertyChanged("List"); }
    } 

现在,您将Collection绑定到ItemsControl

<ScrollViewer  HorizontalAlignment="Left" Width="254" Height="Auto" >
            <ItemsControl x:Name="ItemsControlComputers" ItemsSource="{Binding List, Mode=TwoWay}" Height="Auto"
             HorizontalAlignment="Left" Width="254" ScrollViewer.VerticalScrollBarVisibility="Visible"
            ScrollViewer.HorizontalScrollBarVisibility="Auto"
            Background="{x:Null}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Width="254">
                            <TextBlock Text="{Binding }" Margin="4,0,0,5" VerticalAlignment="Center">      
                            </TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ScrollViewer>

答案 1 :(得分:0)

用这个去错误的轨道而不是创建DependencyProperty我应该刚刚创建了一个普通的属性,但是因为它与UI相关我不想用它的ViewModel。所以我在与我的行为和其他附加属性相同的命名空间中创建了一个具有单例模式的类。这也意味着我可以从任何行为中设置集合。

这是绑定:

<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
}