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