wangwulicsdn 2024-09-07 12:54 采纳率: 46.4%
浏览 18
已结题

WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值

WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值

前端WPF页面

<local:BaseWindow
        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"
        xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        xmlns:vm="clr-namespace:VIVI.DTS.ViewModels"
        xmlns:ucontrols="clr-namespace:VIVI.DTS.UControls"
        xmlns:utils="clr-namespace:VIVI.DTS.Utils"
        xmlns:local="clr-namespace:VIVI.DTS"
        x:Class="VIVI.DTS.MainWindow"
        mc:Ignorable="d"
        Loaded="BaseWindow_Loaded"
        Title="MainWindow" Height="768" Width="1824"  Style="{StaticResource noneBorderWindowStyle}">
    <Window.DataContext>
        <vm:MainWindowViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <utils:FalseConverter x:Key="falseConverter"/>
        <utils:TrueConverter x:Key="trueConverter"/>
    </Window.Resources>
    <Border Style="{StaticResource windowBorderStyle}">
        <DockPanel>
            <Grid DockPanel.Dock="Top" Height="60"  Style="{StaticResource controlGridTopBarStyle}"> 
            </Grid>
            <Grid DockPanel.Dock="Left" Width="220"  Style="{StaticResource ControlGridLeftBlockStyle}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="5px"></RowDefinition>
                    <RowDefinition Height="60px"></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition Height="80px"></RowDefinition>
                    <RowDefinition Height="15px"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="220"></ColumnDefinition>
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0" Grid.Row="1"  Width="220" Height="60px">
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition></ColumnDefinition>
                        <ColumnDefinition Width="28px"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                   <hc:SearchBar Name="SearchBarCustomVerify" HorizontalAlignment="Right"  Grid.Column="0" Grid.Row="0" Grid.RowSpan="2"  Text="{Binding NavMenusKeyWords}" Width="180"  Height="40px" Style="{StaticResource SearchBarPlusStyle}" hc:InfoElement.Placeholder="搜索主菜单"  Command="{Binding FindFirstMenuCmd}"   />

                    <Button x:Name="btnFirstMenuTimeSeries"   VerticalAlignment="Bottom"  Padding="0" Width="20" Height="20" Grid.Column="1" Grid.Row="0"  Style="{StaticResource TimeSeriesBtnStyle}" />
                    <Button x:Name="btnFirstMenuNameSequence" VerticalAlignment="Top" Padding="0" Width="20" Height="20" Grid.Column="1" Grid.Row="1"   Style="{StaticResource NameSequenceBtnStyle}" />
                </Grid> 
            </Grid>
            <Grid DockPanel.Dock="Right" Margin="10px,0,0,0"> 
            </Grid>
        </DockPanel>
    </Border>
</local:BaseWindow>



后台C#代码

using HandyControl.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Win32;
using SqlSugar;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Xps.Packaging;
using VIVI.BLL;
using VIVI.DTS.Utils;
using VIVI.Framework;

using VIVI.Model;

namespace VIVI.DTS.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        protected IAccountService accountService => App.ServiceProvider.GetRequiredService<IAccountService>();
        protected IMenuService menuService => App.ServiceProvider.GetRequiredService<IMenuService>();
        protected IContentService contentService => App.ServiceProvider.GetRequiredService<IContentService>();
        public MainWindowViewModel() : base()
        { 
            InitCommands();

            QueryFirstMenuData();

            QuerySecondMenuData();

            QueryContentData();
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void InitCommands()
        {  
            // 一级菜单查询
            FindFirstMenuCmd = new RelayCommand(o =>
            {
                QueryFirstMenuData();
            });
            
        }

   

        Guid ParentId = Guid.Empty;
        int page = 1; // 第一页
        int pageSize = 5; // 每页5个元素      
        /// 查询一级菜单
        /// </summary>
        private void QueryFirstMenuData()
        {  
            // 一级菜单
            ObservableCollection<Menus> ocMenusList = new ObservableCollection<Menus>();
            QueryModel firstMenuQuery = new QueryModel();
            firstMenuQuery.MenuLevel = EnumMenuLevels.First; 
          
           
            
            // -------------------------- NavMenusKeyWords 和 navMenusKeyWords --------------------------
            // -------------------------- 取不到值 --------------------------
            
            if (!string.IsNullOrEmpty(navMenusKeyWords))
            {
                firstMenuQuery.Key = navMenusKeyWords;
            }
            List<Menus> firstMenuList = menuService.GetList(firstMenuQuery); 
            if (firstMenuList == null|| firstMenuList.Count() <= 0)
            {
                return;
            }
            ParentId = firstMenuList[0].Id;
            ocMenusList = ListHelper.ToObservableCollectionMenus(firstMenuList);
            FirstMenuData = ocMenusList; 
        }  
       
        #endregion

        #region 命令
        //最大化
        public ICommand MaxWindowCmd { get; set; } 
        //导航项选择命令
        public ICommand SelectItemCmd { get; set; }

        /// <summary>
        /// 一级菜单查询命令
        /// </summary>
        public ICommand FindFirstMenuCmd { get; set; } 
        
        #endregion

    }
}


  • 写回答

24条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-09-07 12:54
    关注
    获得0.40元问题酬金

    以下回复引自免费微信小程序:皆我百晓生。 如问题解决,还请采纳。如仍有疑问,请回复留言或私信博主,以便获得更精准的帮助。

    在WPF的MVVM模式下,如果要在HATCHSEARCHBAR控件中绑定NAVMENUSKEYWORDS属性,并且获取到这个值,需要确保你正确地定义了导航菜单的关键词。在C#中,你可以使用HATCHSEARCHBAR组件的TEXT属性来设置一个文本框,然后在视图中显示这个文本框中的内容。以下是一个示例:

    USING SYSTEM;
    USING SYSTEM.WINDOWS.FORMS;
    
    PUBLIC PARTIAL CLASS FORM1 : FORM
    {
        PRIVATE HACHSEARCHBAR HACHSEARCHBAR;
    
        PUBLIC FORM1()
        {
            INITIALIZECOMPONENT();
        }
    
        PRIVATE VOID BTNSEARCH_CLICK(OBJECT SENDER, EVENTARGS E)
        {
            // 设置文本框的内容
            STRING SEARCHTEXT = TXTSEARCH.TEXT;
    
            // 获取当前目录下的所有文件名
            VAR FILES = DIRECTORY.GETFILES("C:\\", "*.TXT");
    
            // 创建一个新的HATCHSEARCHBAR对象并设置其文本框为搜索结果
            HACHSEARCHBAR = NEW HACHSEARCHBAR(FILES);
            HACHSEARCHBAR.SETTEXT(SEARCHTEXT);
    
            // 显示搜索结果
            TXTSEARCH.TEXT = "";
        }
    }
    

    在这个例子中,当用户点击"搜索"按钮时,它会创建一个新的HATCHSEARCHBAR对象并将其设置为搜索结果。然后,通过将搜索结果的文本框中的内容替换回原来的文本框中,我们可以看到输入的搜索关键词被绑定到了NAVMENUSKEYWORDS属性上。

    如果你的问题是关于如何在C#中实现此操作,这里提供了一个基本的示例。请注意,你需要根据你的具体需求和环境调整这些代码以适应你的具体情况。

    评论

报告相同问题?

问题事件

  • 系统已结题 9月15日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见