C# 如何更改WPF ItemComControl中的面板位置?

C# 如何更改WPF ItemComControl中的面板位置?,c#,.net,wpf,C#,.net,Wpf,我的WPF应用程序代码在.cs文件中定义的函数调用上生成面板。代码中使用ItemControl来生成这些面板。我想上下移动面板 示例:有三个面板PanelA、PanelB、PanelC现在每个面板上都有一个按钮可以向上移动。现在我选择了PanleB。我点击那个按钮,PanelB将向上移动,现在他们应该像PanelB,PanelA,PanelC .XAML文件: <ItemsControl x:Name="lstItems" > <ItemsControl.ItemTem

我的WPF应用程序代码在.cs文件中定义的函数调用上生成面板。代码中使用ItemControl来生成这些面板。我想上下移动面板

示例:有三个面板PanelA、PanelB、PanelC现在每个面板上都有一个按钮可以向上移动。现在我选择了PanleB。我点击那个按钮,PanelB将向上移动,现在他们应该像PanelB,PanelA,PanelC

.XAML文件:

<ItemsControl x:Name="lstItems" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" />
                <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
  public partial class MainWindow : Window
  {

  protected ObservableCollection<MyPanel> texts = new ObservableCollection<MyPanel>();

public MainWindow()
{
    InitializeComponent();

    texts.Add(new MyPanel() { Text = "Test 1" });
    texts.Add(new MyPanel() { Text = "Test 2" });

    lstItems.ItemsSource = texts;
   }
}

 public class MyPanel : INotifyPropertyChanged
{
  private string _id;
  private string _text;
  private double _fontSize = 10;

  public string Id
  {
      get { return _id; }
     set
    {
        if (value != _id)
        {
            _id = value;
            NotifyPropertyChanged();
         }
     }
 }
public string Text
{
    get { return _text; }
    set
     {
         if (value != _text)
         {
             _text = value;
             NotifyPropertyChanged();
         }
     }
 }
 public double FontSize
{
    get { return _fontSize; }
    set
    {
        if (value != _fontSize)
        {
            _fontSize = value;
            NotifyPropertyChanged();
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
   <ItemsControl x:Name="lstItemsClassM" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                <ComboBox x:Name="cboOccupation" IsEditable="False"  HorizontalAlignment="Left"
        Text="{Binding Path=Alignment, Mode=TwoWay}"
        Margin="4" Width="140">
                        <ComboBoxItem>Right</ComboBoxItem>
                        <ComboBoxItem>Left</ComboBoxItem>

                    </ComboBox>
                    <Button Content="Move Up" Click="Button_Click_1" Tag="{Binding PKId}"/>
                    <Button Content="{Binding Alignment, Mode=TwoWay}" Click="Button_Click" Tag="{Binding PKId}" SourceUpdated="Button_SourceUpdated" />
                    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" TextAlignment="{Binding Alignment, Mode=OneWay}"  />
                    <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

.CS文件:

<ItemsControl x:Name="lstItems" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" />
                <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
  public partial class MainWindow : Window
  {

  protected ObservableCollection<MyPanel> texts = new ObservableCollection<MyPanel>();

public MainWindow()
{
    InitializeComponent();

    texts.Add(new MyPanel() { Text = "Test 1" });
    texts.Add(new MyPanel() { Text = "Test 2" });

    lstItems.ItemsSource = texts;
   }
}

 public class MyPanel : INotifyPropertyChanged
{
  private string _id;
  private string _text;
  private double _fontSize = 10;

  public string Id
  {
      get { return _id; }
     set
    {
        if (value != _id)
        {
            _id = value;
            NotifyPropertyChanged();
         }
     }
 }
public string Text
{
    get { return _text; }
    set
     {
         if (value != _text)
         {
             _text = value;
             NotifyPropertyChanged();
         }
     }
 }
 public double FontSize
{
    get { return _fontSize; }
    set
    {
        if (value != _fontSize)
        {
            _fontSize = value;
            NotifyPropertyChanged();
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
   <ItemsControl x:Name="lstItemsClassM" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                <ComboBox x:Name="cboOccupation" IsEditable="False"  HorizontalAlignment="Left"
        Text="{Binding Path=Alignment, Mode=TwoWay}"
        Margin="4" Width="140">
                        <ComboBoxItem>Right</ComboBoxItem>
                        <ComboBoxItem>Left</ComboBoxItem>

                    </ComboBox>
                    <Button Content="Move Up" Click="Button_Click_1" Tag="{Binding PKId}"/>
                    <Button Content="{Binding Alignment, Mode=TwoWay}" Click="Button_Click" Tag="{Binding PKId}" SourceUpdated="Button_SourceUpdated" />
                    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" TextAlignment="{Binding Alignment, Mode=OneWay}"  />
                    <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
公共部分类主窗口:窗口
{
受保护的ObservableCollection文本=新的ObservableCollection();
公共主窗口()
{
初始化组件();
添加(newmypanel(){Text=“test1”});
添加(newmypanel(){Text=“test2”});
lstItems.ItemsSource=文本;
}
}
公共类MyPanel:INotifyPropertyChanged
{
私有字符串_id;
私有字符串_文本;
私人双人房_fontSize=10;
公共字符串Id
{
获取{return\u id;}
设置
{
如果(值!=\u id)
{
_id=值;
NotifyPropertyChanged();
}
}
}
公共字符串文本
{
获取{return\u text;}
设置
{
如果(值!=\u文本)
{
_文本=值;
NotifyPropertyChanged();
}
}
}
公共双字号
{
获取{return\fontSize;}
设置
{
如果(值!=\u fontSize)
{
_fontSize=值;
NotifyPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

编辑: XAML文件:

<ItemsControl x:Name="lstItems" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" />
                <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
  public partial class MainWindow : Window
  {

  protected ObservableCollection<MyPanel> texts = new ObservableCollection<MyPanel>();

public MainWindow()
{
    InitializeComponent();

    texts.Add(new MyPanel() { Text = "Test 1" });
    texts.Add(new MyPanel() { Text = "Test 2" });

    lstItems.ItemsSource = texts;
   }
}

 public class MyPanel : INotifyPropertyChanged
{
  private string _id;
  private string _text;
  private double _fontSize = 10;

  public string Id
  {
      get { return _id; }
     set
    {
        if (value != _id)
        {
            _id = value;
            NotifyPropertyChanged();
         }
     }
 }
public string Text
{
    get { return _text; }
    set
     {
         if (value != _text)
         {
             _text = value;
             NotifyPropertyChanged();
         }
     }
 }
 public double FontSize
{
    get { return _fontSize; }
    set
    {
        if (value != _fontSize)
        {
            _fontSize = value;
            NotifyPropertyChanged();
        }
    }
}

public event PropertyChangedEventHandler PropertyChanged;

protected void NotifyPropertyChanged(String propertyName = "")
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
   <ItemsControl x:Name="lstItemsClassM" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                <ComboBox x:Name="cboOccupation" IsEditable="False"  HorizontalAlignment="Left"
        Text="{Binding Path=Alignment, Mode=TwoWay}"
        Margin="4" Width="140">
                        <ComboBoxItem>Right</ComboBoxItem>
                        <ComboBoxItem>Left</ComboBoxItem>

                    </ComboBox>
                    <Button Content="Move Up" Click="Button_Click_1" Tag="{Binding PKId}"/>
                    <Button Content="{Binding Alignment, Mode=TwoWay}" Click="Button_Click" Tag="{Binding PKId}" SourceUpdated="Button_SourceUpdated" />
                    <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" TextAlignment="{Binding Alignment, Mode=OneWay}"  />
                    <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

赖特
左边
.CS文件:

 public partial class Window2 : Window
{
    protected ObservableCollection<ClassM> texts = new ObservableCollection<ClassM>();
    int dv;
    public Window2()
    {
        InitializeComponent();
        dv=1;
        texts.Add(new ClassM() { PKId=dv, Text = "Test 1" });
        dv=2;
        texts.Add(new ClassM() { PKId=dv, Text = "Test 2" });

        lstItemsClassM.ItemsSource = texts;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var myValue = ((Button)sender).Tag;
            foreach (var f in texts.ToList())
            {
                if (f.PKId.ToString() == myValue.ToString())
                {
                    f._alignment = "Right";
                    MessageBox.Show(f._alignment);
                }
            }

    }

    private void Button_SourceUpdated(object sender, DataTransferEventArgs e)
    {
        var myValue = ((Button)sender).Tag;
        foreach (var f in texts.ToList())
        {
            if (f.PKId.ToString() == myValue.ToString())
            {
                f._alignment = "Right";
                MessageBox.Show(f._alignment);
            }
        }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        var myValue = ((Button)sender).Tag;
       foreach (var f in texts.ToList())
        {
            if (f.PKId.ToString() == myValue.ToString())
            {
                int s = f.PKId + 1;
                texts.Move(f.PKId , s);

                MessageBox.Show(f.PKId +"  &&&&& " + s );
            }
        }

    }


}


public class ClassM : INotifyPropertyChanged
{
    private string _id;
    private int _pkid;
    private string _text;
    private double _fontSize = 10;
    public bool _isChecked { get; set; }
    public string _alignment="Left";

    public int PKId
    {
        get { return _pkid; }
        set
        {
            if (value != _pkid)
            {
                _pkid = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Id
    {
        get { return _id; }
        set
        {
            if (value != _id)
            {
                _id = value;
                NotifyPropertyChanged();
            }
        }
    }

    public bool IsChecked
    {
        get { return _isChecked; }
        set
        {
            if (value != _isChecked)
            {
                _isChecked = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Text
    {
        get { return _text; }
        set
        {
            if (value != _text)
            {
                _text = value;
                NotifyPropertyChanged();
            }
        }
    }
    public double FontSize
    {
        get { return _fontSize; }
        set
        {
            if (value != _fontSize)
            {
                _fontSize = value;
                NotifyPropertyChanged();
            }
        }
    }
    public string Alignment
    {
        get { return _alignment; }
        set
        {
            if (value != _alignment)
            {
                _alignment = value;
                NotifyPropertyChanged();
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
公共部分类窗口2:窗口
{
受保护的ObservableCollection文本=新的ObservableCollection();
int-dv;
公共窗口2()
{
初始化组件();
dv=1;
Add(new ClassM(){PKId=dv,Text=“Test 1”});
dv=2;
Add(new ClassM(){PKId=dv,Text=“Test 2”});
lstItemsClassM.ItemsSource=文本;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
var myValue=((按钮)sender).Tag;
foreach(text.ToList()中的var f)
{
如果(f.PKId.ToString()==myValue.ToString())
{
f、 _alignment=“Right”;
MessageBox.Show(f.\u对齐);
}
}
}
私有无效按钮\u源已更新(对象发送方、数据传输目标)
{
var myValue=((按钮)sender).Tag;
foreach(text.ToList()中的var f)
{
如果(f.PKId.ToString()==myValue.ToString())
{
f、 _alignment=“Right”;
MessageBox.Show(f.\u对齐);
}
}
}
私有无效按钮\u单击\u 1(对象发送者,路由目标)
{
var myValue=((按钮)sender).Tag;
foreach(text.ToList()中的var f)
{
如果(f.PKId.ToString()==myValue.ToString())
{
ints=f.PKId+1;
移动文本(f.PKId,s);
MessageBox.Show(f.PKId+“&&&&&&&”+s);
}
}
}
}
公共类ClassM:INotifyPropertyChanged
{
私有字符串_id;
私人密码匙;
私有字符串_文本;
私人双人房_fontSize=10;
公共布尔值已检查{get;set;}
公共字符串_alignment=“Left”;
公共密钥基础设施
{
获取{return}pkid;}
设置
{
如果(值!=\u pkid)
{
_pkid=值;
NotifyPropertyChanged();
}
}
}
公共字符串Id
{
获取{return\u id;}
设置
{
如果(值!=\u id)
{
_id=值;
NotifyPropertyChanged();
}
}
}
公共场所被检查
{
获取{return\u已检查;}
设置
{
如果(值!=\u已检查)
{
_isChecked=值;
NotifyPropertyChanged();
}
}
}
公共字符串文本
{
获取{return\u text;}
设置
{
如果(值!=\u文本)
{
_文本=值;
NotifyPropertyChanged();
}
}
}
公共双字号
{
获取{return\fontSize;}
设置
{
如果(值!=\u fontSize)
{
_fontSize=值;
NotifyPropertyChanged();
}
}
}
公共字符串对齐
{
获取{return\u alignment;}
设置
{
如果(值!=\u对齐)
{
_对齐=值;
NotifyPropertyChanged();
}
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName=”“)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

}

使用ObservableCollection的
Move
方法:

private void Swap(MyPanel a, MyPanel b)
{
    var indexA = texts.IndexOf(a);
    var indexB = texts.IndexOf(b);

    texts.Move(a,b);
}

在主窗口中创建命令,比如MovePanelCommand,并将按钮commmand绑定到此命令,并将当前项作为命令参数发送

<StackPanel Orientation="Vertical">
                <TextBox x:Name="txtText" Width="300" Height="100" Text="{Binding Text;, Mode=TwoWay}" FontSize="{Binding FontSize, Mode=OneWay}" />
                <Slider Minimum="10" Maximum="30" Value="{Binding FontSize, Mode=TwoWay}" />
                <Button Command="{Binding DataContext.MovePanelCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" CommandParamter="{Binding}"/>
 </StackPanel>
谢谢。因为我使用的是标签w