SearchPanel.xaml_1118

本文介绍了一个具备拖放功能的查询界面的设计与实现,包括界面布局、拖放事件处理及查询逻辑。通过XAML定义了用户界面,并在后台代码中实现了数据加载和查询功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台

<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"  x:Class="ComputerDropDragControl.SearchPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"  Width="200" Height="1010" >
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,10,0,0">
            <TextBlock Text="设备名称:" VerticalAlignment="Center"></TextBlock>
            <TextBox Width="125" Height="20" x:Name="txtNodeName"></TextBox>
        </StackPanel>
        <Button x:Name="btnSearch" Grid.Row="1" Width="50" Height="25" Content="查询" HorizontalAlignment="Right"
                 Click="btnSearch_Click" Margin="0,0,10,0"></Button>
        <StackPanel Grid.Row="2">
            <!--<ScrollViewer x:Name="scrollOne"
                          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
                          Height="100" Width="180" Margin="10,0,0,0" Background="#333333"
                          HorizontalAlignment="Left" VerticalAlignment="Top">-->
            <toolkit:ListBoxDragDropTarget x:Name="listDrag" AllowDrop="False"
                                               HorizontalAlignment="Left" VerticalAlignment="Top"
                                           ItemDragStarting="listDrag_ItemDragStarting" ItemDroppedOnTarget="listDrag_ItemDroppedOnTarget" >
                <ListBox Width="180" x:Name="listComputer" Margin="10,0,0,0"
                               ScrollViewer.HorizontalScrollBarVisibility="Auto"
                             ScrollViewer.VerticalScrollBarVisibility="Auto">

                </ListBox>
            </toolkit:ListBoxDragDropTarget>
            <!--</ScrollViewer>-->
        </StackPanel>
    </Grid>
</UserControl>

后台

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Browser;

namespace ComputerDropDragControl
{
    /// <summary>
    /// “查询界面”
    /// 创建人:吴兆娟
    /// 创建时间:2011-11-1
    /// </summary>
    public partial class SearchPanel : UserControl
    {
        #region <<页面加载>>

        /// <summary>
        /// 页面加载
        /// </summary>
        public SearchPanel()
        {
            InitializeComponent();

            //初始化表单
            InitForm();

            //加载数据
            LoadData();
        }

        /// <summary>
        /// 初始化表单
        /// </summary>
        private void InitForm()
        {
            //获取浏览器分辨率信息
            ScriptObject screen = (ScriptObject)HtmlPage.Window.GetProperty("screen");
            double width = (double)screen.GetProperty("width");
            double height = (double)screen.GetProperty("height");
            listComputer.Height = height - 25 * 3 - (10 + 20 + 25 + 10) - 38;
        }

        #endregion

        #region <<控件事件>>

        #region <<拖拽相关事件>>

        /// <summary>
        /// “拖”事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listDrag_ItemDragStarting(object sender, ItemDragEventArgs e)
        {
            StaticClass.FromList = true;
            StaticClass.DragPanelList = (ListBoxDragDropTarget)sender;

            ListBox list = (ListBox)this.listDrag.Content;
            ListBoxItem selectItem = (ListBoxItem)list.SelectedItem;
            int index = (int)list.SelectedIndex;
            SelectListItem = selectItem;
            SelectIndex = index;
        }

        private void listDrag_ItemDroppedOnTarget(object sender, ItemDragEventArgs e)
        {
            if (StaticClass.DropPanel != null)
            {
                if (StaticClass.DropPanel.AllowDrop)
                {
                    if (!StaticClass.ExistPlace)
                    {
                        ((StackPanel)StaticClass.DropPanel.Content).Children.RemoveAt(3);
                        StaticClass.DropPanel.AllowDrop = true;

                        ListBox list = (ListBox)this.listDrag.Content;
                        list.Items.Insert(SelectIndex, SelectListItem);
                    }
                }
            }
        }

        #endregion

        /// <summary>
        /// “查询”按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            List<NodeFrameView> result = nList.ToList<NodeFrameView>();
            if (!string.IsNullOrEmpty(this.txtNodeName.Text))
            {
                result = (from nodeFrameView in nList
                          where nodeFrameView.NodeName.IndexOf(this.txtNodeName.Text.Trim()) > 0
                          select nodeFrameView).ToList<NodeFrameView>();
            }
            listComputer.Items.Clear();
            foreach (NodeFrameView item in result)
            {
                ListBoxItem li = new ListBoxItem();
                li.Content = item.NodeName;
                li.DataContext = item;
                listComputer.Items.Add(li);
            }
        }
        #endregion

        #region <<辅助方法>>

        public ObservableCollection<NodeFrameView> nList;

        private ObservableCollection<NodeFrameView> LoadData()
        {
            return nList = new ObservableCollection<NodeFrameView>
            {
                new NodeFrameView("n1","List设备一",2),
                new NodeFrameView("n2","List设备二",3),
                new NodeFrameView("n3","List设备三",3),

                new NodeFrameView("n4","List设备五",3),
                new NodeFrameView("n5","List设备六",2),
                new NodeFrameView("n6","List设备七",1),
                new NodeFrameView("n7","List设备八",2)
            };
        }

        #endregion


        public ListBoxItem SelectListItem { get; set; }

        public int SelectIndex { get; set; }
    }
}

 

<think>我们正在处理用户关于ICSharpCode.AvalonEdit的查询。根据用户需求,我们需要提供使用指南、文档和示例。 由于我们有一些引用内容,但引用[1]直接与AvalonEdit相关,引用[2]是关于AgileEAS.NET的,不相关。因此,我们主要参考引用[1]。 引用[1]提到:在XAML中引入命名空间和使用方法,以及一些应用案例和最佳实践。 我们需要组织回答: 1. 介绍AvalonEdit 2. 如何安装(通过NuGet) 3. 基本使用:在XAML中添加控件,并设置属性 4. 代码示例(XAML和C#) 5. 常见功能:语法高亮、行号显示等 6. 文档和资源链接 注意:用户要求使用中文回答,并且按照系统级指令处理数学公式(但这里可能不需要数学公式,所以忽略)。 我们将按照以下结构回答: - 简介 - 安装 - 基本使用(XAML和代码) - 高级功能(语法高亮、折叠等) - 示例代码 - 相关文档和资源 - 生成相关问题 由于引用[1]提供了XAML命名空间引入的示例,我们将使用它。 另外,我们可能还需要提供一些常见的功能设置,比如显示行号、设置语法高亮等。 注意:用户要求生成相关问题,放在最后。</think>### ICSharpCode AvalonEdit 使用指南与示例 ICSharpCode.AvalonEdit 是一个开源的 WPF 文本编辑器控件,支持语法高亮、代码折叠、行号显示等高级功能。以下是核心使用方法和示例: --- #### **1. 安装与基础配置** - **通过 NuGet 安装**: ```bash Install-Package AvalonEdit ``` - **在 XAML 中引入控件**(关键步骤)[^1]: ```xml xmlns:avalon="clr-namespace:ICSharpCode.AvalonEdit;assembly=AvalonEdit" <avalon:TextEditor x:Name="textEditor" FontFamily="Consolas" FontSize="12"/> ``` --- #### **2. 核心功能实现** ##### **语法高亮**(以 C# 为例) ```csharp // 添加高亮规则 var syntaxHighlighting = HighlightingManager.Instance.GetDefinition("C#"); textEditor.SyntaxHighlighting = syntaxHighlighting; ``` ##### **显示行号与代码折叠** ```xml <avalon:TextEditor ShowLineNumbers="True" WordWrap="True"> <avalon:TextEditor.TextArea> <avalon:TextArea> <avalon:TextArea.LeftMargins> <avalon:LineNumberMargin/> <avalon:IconBarMargin/> </avalon:TextArea.LeftMargins> </avalon:TextArea> </avalon:TextEditor.TextArea> </avalon:TextEditor> ``` ##### **自定义快捷键**(例如 Ctrl+S 保存) ```csharp textEditor.TextArea.KeyDown += (sender, e) => { if (e.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control) { SaveContent(textEditor.Text); e.Handled = true; } }; ``` --- #### **3. 完整示例:简易代码编辑器** ```xml <!-- MainWindow.xaml --> <Window xmlns:avalon="clr-namespace:ICSharpCode.AvalonEdit;assembly=AvalonEdit"> <Grid> <avalon:TextEditor x:Name="codeEditor" ShowLineNumbers="True" SyntaxHighlighting="C#"/> </Grid> </Window> ``` ```csharp // MainWindow.xaml.cs public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 加载初始代码 codeEditor.Text = "using System;\n\npublic class Program {\n\tpublic static void Main() {\n\t\tConsole.WriteLine(\"Hello AvalonEdit!\");\n\t}\n}"; } } ``` --- #### **4. 高级功能** - **代码折叠**:使用 `FoldingManager` 实现折叠区域管理。 - **搜索替换**:通过 `SearchPanel.Install(textEditor.TextArea)` 添加搜索框。 - **自定义高亮**:继承 `IHighlightingDefinition` 接口创建新语法规则。 --- #### **5. 官方文档与资源** - [GitHub 仓库](https://github.com/icsharpcode/AvalonEdit):源码与基础示例 - [官方文档](https://avalonedit.net/documentation):详细 API 说明 - 应用案例:在 [SharpDevelop](https://github.com/icsharpcode/SharpDevelop) IDE 中查看完整实现。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值