【问题标题】:Wpf form to pull elements from left to rightWpf表单从左到右拉元素
【发布时间】:2013-05-14 02:19:57
【问题描述】:

我想制作一个与我分享的类似的屏幕。这个想法是从左到右拉项目。我浏览了 WPF 工具箱,并没有找到完全符合此要求的小部件。或者这只是 2 个简单小部件的组合,其中 >> 充当助手。

谁能告诉我这是什么类型的小部件以及如何去做?我试过搜索,但找不到好的搜索词:-((我什至找不到一个好的问题标题)

【问题讨论】:

  • 这只是两个数据网格/列表视图。该按钮从列表一中获取 selectedItems 并移动(添加到右侧,从左侧删除)到新列表。
  • @Jras 谢谢。有用的。我会这样去做。完成后我会更新。希望我能分享一些好的代码:-)
  • @RockStar 在 UX 方面,我个人认为界面真的很差,并且几乎可以解决旧技术的限制。你为什么不只用一个安全/不安全的ComboBox 或只是一个CheckBox 做一个列表框?我们已经用带有 CheckBox 的单个列表和类似的东西替换了我们产品的早期版本(VB6 + 一些 winforms)中的很多此类东西。
  • @HighCore 谢谢我会和我们的建筑师一起提出来。 UI 并非完全由 .感谢您的帮助。

标签: c# .net wpf user-interface


【解决方案1】:

没有像上面这样的预定义控件,但制作起来应该很简单

这里有一个基本大纲可以帮助您入门。

Xaml:

<Window xmlns:Controls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="WPFListBoxGroupTest.MainWindow"
        Title="MainWindow" Height="438" Width="557"  x:Name="UI">
    <Grid DataContext="{Binding ElementName=UI}" >
        <Grid.RowDefinitions>
            <RowDefinition Height="181*"/>
            <RowDefinition Height="23*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="240*"/>
            <ColumnDefinition Width="68*"/>
            <ColumnDefinition Width="241*"/>
        </Grid.ColumnDefinitions>
        <Button Content=">>" Grid.Column="1" Command="{Binding AddDevice}" CommandParameter="{Binding SelectedItem, ElementName=unSecure}" HorizontalAlignment="Center" VerticalAlignment="Center" Height="33"  Width="48"/>
        <DockPanel >
            <TextBox DockPanel.Dock="Top" Text="Unsecured Devices" />
            <DataGrid x:Name="unSecure" ItemsSource="{Binding UnsecuredDevices}" />
        </DockPanel>
        <DockPanel  Grid.Column="2">
            <TextBox DockPanel.Dock="Top" Text="Secured Devices" />
            <DataGrid ItemsSource="{Binding SecuredDevices}" />
        </DockPanel>
    </Grid>
</Window>

代码:

public partial class MainWindow : Window
{
    private ObservableCollection<Device> _unsecuredDevices = new ObservableCollection<Device>();
    private ObservableCollection<Device> _securedDevices = new ObservableCollection<Device>();

    public MainWindow()
    {
        AddDevice = new RelayCommand(o => SecuredDevices.Add(o as Device), o => o != null);
        InitializeComponent();
        UnsecuredDevices.Add(new Device { Name = "Jonathan Mac", MacAddress = "00:1A:8C:B9:CC" });
        UnsecuredDevices.Add(new Device { Name = "Jonathan Mobile", MacAddress = "00:1A:8C:B9:CC" });
        UnsecuredDevices.Add(new Device { Name = "Samsung S3", MacAddress = "00:1A:8C:B9:CC" });
        UnsecuredDevices.Add(new Device { Name = "BlackBerry BB102", MacAddress = "00:1A:8C:B9:CC" });
    }

    public ICommand AddDevice { get; set; }

    public ObservableCollection<Device> UnsecuredDevices
    {
        get { return _unsecuredDevices; }
        set { _unsecuredDevices = value; }
    }

    public ObservableCollection<Device> SecuredDevices
    {
        get { return _securedDevices; }
        set { _securedDevices = value; }
    }
}

public class Device 
{
    public string Name { get; set; }
    public string MacAddress { get; set; }
}

结果:

【讨论】:

  • 1+ 很好的例子。除了 Resharper 会将那些 ObservableCollections 标记为“转换为自动属性”(只需 Alt+Enter+Enter+Enter)=P
  • @HighCore,我不使用 reshaper,我发现它是为 VS 开发的最烦人的扩展,但这只是我 :)
  • @sa_ddam213 我没想到会有这么好的例子。谢谢一百万。
  • @sa_ddam213 如果没有为 VS 开发的最佳功能,我简直无法生存,那就是 R# 能够转换自动属性 ​​"to property with backing field"
  • @HighCore,我使用 CodeSnippets 处理所有垃圾,但我是办公室里唯一一个讨厌 R# 的人,所以也许只有我有点古怪
猜你喜欢
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 2023-02-01
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多