【WPF/WAF】主界面(ShellWindow)引入别的界面布局
问题:主界面如果只用一个布局文件ShellWindow.xaml,会写得很大很臃肿。需要分为多个布局文件,然后由主界面引入。参考http://waf.codeplex.com/官方的BookLibrary案例,别人也是这么做的。
使用WPF Application Framework (WAF)框架新建的项目,将模板的分包结构如下:
需求:在主界面ShellWindow右侧是分页栏(TabControl),每个分页栏内容是一个单独的XAML布局文件。所以现在测试往主界面引入一个界面。完成后的效果应该是这样的:
根据WAF项目的结构,最后看起来是下下图那样,红框中是新增的文件。
由于篇幅有限,只提及几个关键点。完整流程点这里下载。
ShellWindow.xaml关键部分:
<!-- 右侧顶部分页栏/分组栏 -->
<TabControl x:Name="tabControl" DockPanel.Dock="Top" Margin="410,0,10,0" Height="300" VerticalAlignment="Top">
<TabItem Header="户型">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ContentControl Name="HouseTypeView" Content="{Binding ShellService.HouseTypeView}" Margin="10" Focusable="False"/>
</ScrollViewer>
</TabItem>
......
新建的分页栏某一页的布局HouseTypeView.xaml:
<UserControl
x:Class="WafApplication1.Presentation.Views.HouseTypeView"
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:vm="clr-namespace:WafApplication1.Applications.ViewModels"
xmlns:local="clr-namespace:WafApplication1"
mc:Ignorable="d" MinWidth="300" MinHeight="270"
>
<DockPanel Margin="10" >
<StackPanel Orientation="Vertical">
<Grid Margin="10" Height="40" >
<!-- 定义列宽 -->
<Grid.ColumnDefinitions>
<!-- 第一列 -->
<ColumnDefinition Width="100"/>
<!-- 第二列 -->
<ColumnDefinition Width="200"/>
<!-- 第三列 -->
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<!-- 第一行第一列 -->
<ComboBox Grid.Column="0" Grid.Row="0" x:Name="cityComboxBox" Margin="50,10,0,200" Height="20"/>
<!-- 第一行第二列 -->
<ComboBox Grid.Column="1" Grid.Row="0" x:Name="communityComboxBox" Height="20" Margin="10,10,0,200" />
<!-- 第一行第三列 -->
<Button Grid.Column="2" Grid.Row="0" x:Name="searchBtn" Command = "{Binding AlertCommand}" Content="搜 索" VerticalAlignment="Center" Width="50" Height="20" Background="#0094ff" Margin="10,10,0,200"/>
</Grid>
<Grid Height="100">
<StackPanel Orientation="Horizontal" Margin="10, 10, 10, 10">
<TextBlock Text="热门搜索 :" FontSize="10" Margin="70,5,20,62" HorizontalAlignment="Center" />
<WrapPanel Orientation="Horizontal" Width="250">
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
<TextBox Text="A社区" Margin="5" />
</WrapPanel>
</StackPanel>
</Grid>
</StackPanel>
</DockPanel>
</UserControl>
HouseTypeView.xaml.cs主要是做两个ComboBox下拉菜单的级联:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using WafApplication1.Applications.Views;
using WafApplication1.Applications.ViewModels;
namespace WafApplication1.Presentation.Views
{
[Export(typeof(IHouseTypeView))]
public partial class HouseTypeView : UserControl, IHouseTypeView
{
private Dictionary<string, string[]> cityAndCommunityDictionary = new Dictionary<string, string[]>()
{
{ "南宁", new string[] { "南宁A社区", "南宁B社区", } },
{ "柳州", new string[] { "柳州A社区", "柳州B社区", "柳州C社区", "柳州D社区" } },
{ "桂林", new string[] { "桂林A社区", "桂林B社区", "桂林C社区" } },
};
public HouseTypeView()
{
InitializeComponent();
// 初始化两个下拉列表
InitComboBox();
}
/// <summary>
/// 初始化选择城市的下拉列表
/// </summary>
private void InitComboBox()
{
// 初始化城市列表
ItemCollection coll = cityComboxBox.Items;
foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
{
ComboBoxItem boxItem = new ComboBoxItem() { Content = kvp.Key };
coll.Add(boxItem);
}
// 给ComboBox注册一个选项改变的事件
cityComboxBox.SelectionChanged += new SelectionChangedEventHandler(cityComboxBox_SelectionChanged);
}
private void cityComboxBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// 当前城市的社区
ItemCollection coll = communityComboxBox.Items;
// 先清空
coll.Clear();
// 再添加
foreach (KeyValuePair<string, string[]> kvp in cityAndCommunityDictionary)
{
// kvp.Value = { "南宁A社区", "南宁B社区", }
// 此时的 cityComboxBox.SelectedValue = System.Windows.Controls.ComboBoxItem: 南宁
// 所以如果用这种方法获取选中的值,还需要切割字符串
ComboBoxItem selectedCity = cityComboxBox.SelectedItem as ComboBoxItem;
string cityName = selectedCity.Content.ToString();
System.Console.WriteLine("cityName = " + cityName);
if (cityName.Equals(kvp.Key))
{
foreach (var item in kvp.Value)
{
// item = "南宁A社区"
ComboBoxItem boxItem = new ComboBoxItem() { Content = item };
coll.Add(boxItem);
}
}
}
}
}
}
在Applications/Controllers新建一个HouseTypeController.cs:
using System.ComponentModel.Composition;
using WafApplication1.Applications.Services;
using WafApplication1.Applications.ViewModels;
namespace WafApplication1.Applications.Controllers
{
[Export]
internal class HouseTypeController
{
private readonly IShellService shellService;
private readonly HouseTypeViewModel houseTypeViewModel; // 引入
[ImportingConstructor]
public HouseTypeController(IShellService shellService, HouseTypeViewModel houseTypeViewModel)
{
this.shellService = shellService;
this.houseTypeViewModel = houseTypeViewModel; // 构造传参引入
}
public void Initialize()
{
shellService.HouseTypeView = houseTypeViewModel.View; //
}
}
}
修改Applications/Controllers/ApplicationControl.cs:
using System.ComponentModel.Composition;
using WafApplication1.Applications.Services;
using WafApplication1.Applications.ViewModels;
namespace WafApplication1.Applications.Controllers
{
[Export]
internal class ApplicationController
{
private readonly ShellService shellService;
private readonly ShellViewModel shellViewModel;
private readonly HouseTypeViewModel houseTypeViewModel;
private readonly HouseTypeController houseTypeController;
[ImportingConstructor]
public ApplicationController(ShellViewModel shellViewModel, ShellService shellService, HouseTypeViewModel houseTypeViewModel, HouseTypeController houseTypeController)
{
this.shellViewModel = shellViewModel;
this.shellService = shellService;
this.houseTypeViewModel = houseTypeViewModel;
this.houseTypeController = houseTypeController;
}
public void Initialize()
{
shellService.ShellView = shellViewModel.View;
houseTypeController.Initialize();
}
public void Run()
{
shellViewModel.Show();
}
public void Shutdown()
{
}
}
}