我在WPF和Caliburn.Micro方面的掌握程度相对较低。
我的目标是将组合框选定项的绑定从ShellView的代码后台移动到视图模型中,与组合框的项目集合已经实现的方式相同。
XAML:
<Window x:Class="EomDatabaseUtility.Views.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Eom Tool Database Utility" Height="350" Width="525">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="258" HorizontalAlignment="Left" Margin="12,41,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="479" />
<Button Content="Execute" Height="23" HorizontalAlignment="Left" Margin="416,12,0,0" VerticalAlignment="Top" Width="75" x:Name="Execute" />
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" VerticalAlignment="Top" Width="120" x:Name="CatalogName" SelectedValuePath="{Binding Path=SelectedCatalogName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</Window>
代码后台(如果我理解正确的话,目标是不需要添加任何代码):
namespace EomDatabaseUtility.Views
{
using System.Windows;
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
}
// --> This should go in the view model, Right?
private string selectedCatalogName;
public string SelectedCatalogName
{
get { return selectedCatalogName; }
set { selectedCatalogName = value; }
}
}
}
视图模型(目前作为下拉列表框的条目集合提供者以及一个按钮事件处理程序):
namespace EomDatabaseUtility.ViewModels
{
using Caliburn.Micro;
using System.Collections.Generic;
public class ShellViewModel : PropertyChangedBase
{
public List<string> CatalogName
{
get
{
return new List<string> { "foo", "bar" };
}
}
public void Execute()
{
System.Windows.MessageBox.Show("hello");
}
}
}