Dragable objects in WPF in an ItemsControl?

前端 未结 2 802
广开言路
广开言路 2021-01-05 23:01

I want to be able to implement an ItemsControl with dragable items. The reason for the ItemsControl is so I can bind to my ViewModel in the background.

I\'ve tried

相关标签:
2条回答
  • 2021-01-05 23:24

    Ben I didn't think that approach worked at first but after more experimenting I got it.

    The problem could be boiled down to: Canvas.Top and Canvas.Left don't work while in an items control. But you are correct that the style is the way to get around the problem. Here is the solution I came up with:

    <ItemsControl ItemsSource="{Binding Notes}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <Canvas />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Thumb Width="150" Height="150" DragDelta="Thumb_DragDelta" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemContainerStyle>
                <Style>
                    <Setter Property="Canvas.Left" Value="{Binding X}" />
                    <Setter Property="Canvas.Top" Value="{Binding Y}" />
                </Style>
            </ItemsControl.ItemContainerStyle>
        </ItemsControl>
    

    And the codebehind:

     public partial class MainWindow : Window
    {
        public ObservableCollection<Note> Notes { get; set; }
    
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = this;
    
            Notes = new ObservableCollection<Note>();
            Notes.Add(new Note(){Title="test", X=100, Y=0});
        }
    
        private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            Note n = (Note)((FrameworkElement)sender).DataContext;
            n.X += e.HorizontalChange;
            n.Y += e.VerticalChange;
        }
    }
    
    public class Note : INotifyPropertyChanged
    {
        private string title;
        private double x;
        private double y;
    
        public double Y
        {
            get { return y; }
            set
            {
                y = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Y"));
            }
        }
    
        public double X
        {
            get { return x; }
            set
            {
                x = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("X"));
            }
        }
    
    
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
    0 讨论(0)
  • 2021-01-05 23:33

    Try setting up the Canvas binding properties via a style

    <Style x:Key="ThumbStyle">
      <Setter Property="Canvas.Left" Value="{Binding Path=Left}"/>
      <Setter Property="Canvas.Top" Value="{Binding Path=Top}"/>
      <!-- more if required -->
    
    </Style>
    

    Then the thumb becomes:

    <Thumb Style={StaticResource ThumbStyle}" ... />

    0 讨论(0)
提交回复
热议问题