0

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

示例:现在有三个面板 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>

.CS 文件:

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

}

编辑:XAML 文件:

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

}

4

2 回答 2

2

使用 ObservableCollection 的Move方法:

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

    texts.Move(a,b);
}
于 2013-10-04T15:54:30.870 回答
0

在您的 MainWindow 中创建命令,例如 MovePanelCommand 并将 Button 命令绑定到此命令,并将当前项目作为命令参数发送

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

现在在命令处理程序中,您将获得要向上移动的面板

private void MovePanelCommandHandler(object param)
{
    MyPanel panel = param as MyPanel;
  // Just move the panel one index up with validation if it is a first panel
}
于 2013-10-04T15:54:58.027 回答