是WPF的新手。我正在使用Prism 4和MahApp Metro创建一个带MVVM的WPF 4.5应用程序。在页面中,我有一个组合框,其中将显示项目列表,并在SelectedItem
我想在下面的表单中显示该项目的详细信息。
现在这里是 ViewEditItemsView.xaml
的代码<UserControl x:Class="WPFSample.Module.Item.View.ViewEditItemsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
xmlns:local="clr-namespace:WPFSample.Module.Item.ViewModel"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="350">
<UserControl.DataContext>
<local:ViewEditItemsViewModel/>
</UserControl.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="40"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock x:Name="tbkSearch" Grid.Column="0" Grid.Row="0" Text="Search Item" VerticalAlignment="Top" Margin="0,14,0,0" HorizontalAlignment="Right" Width="70" />
<ComboBox Width="200" Grid.Column="1" Grid.Row="0" VerticalAlignment="Top"
Margin="0, 10, 0, 0"
Style="{DynamicResource VirtualisedMetroComboBox}"
Controls:TextBoxHelper.Watermark="Autocompletion"
DisplayMemberPath="ItemName"
IsEditable="True"
ItemsSource="{Binding Items}"
MaxDropDownHeight="125"
Text="{Binding Path=ItemName}"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"/>
<Button x:Name="PART_ForwardButton" Grid.Column="2" Grid.Row="0"
DockPanel.Dock="Right"
Style="{DynamicResource MetroCircleButtonStyle}"
Height="40"
Width="40"
FontFamily="Segoe UI Symbol"
FontSize="16"
VerticalAlignment="Top"
HorizontalAlignment="Left"
IsEnabled="True">
<Rectangle Width="20"
Height="15">
<Rectangle.Fill>
<VisualBrush Stretch="Fill">
<VisualBrush.Visual>
<Canvas Width="48"
Height="48"
Clip="F1 M 0,0L 48,0L 48,48L 0,48L 0,0"
UseLayoutRounding="False">
<Path Width="25" ToolTip="Add File"
Height="18"
Canvas.Left="12"
Canvas.Top="15"
Stretch="Fill"
Fill="{DynamicResource BlackBrush}"
Data="F1 M 35,19L 41,19L 41,35L 57,35L 57,41L 41,41L 41,57L 35,57L 35,41L 19,41L 19,35L 35,35L 35,19 Z " />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Button>
<Grid x:Name="grdFileDetails" Grid.ColumnSpan="2" Grid.Column="0" Grid.Row="1" DataContext="{Binding SelectedItem}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="tbkItemName" Grid.Column="0" Text="Item Name" VerticalAlignment="Top" Margin="0,26,197,0" HorizontalAlignment="Right" Width="70" Grid.ColumnSpan="2" />
<TextBox x:Name="txtItemName" Text="{Binding Path=ItemName,Mode=OneWay}" Grid.Column="1" IsEnabled="False"/>
</Grid>
</Grid>
ViewEditItemsViewModel.cs
的代码public class ViewEditItemsViewModel : ViewModelBase
{
private ObservableCollection<ItemViewModel> items;
private ItemViewModel selectedItem;
public ViewEditItemsViewModel()
{
var itemModel = new ItemModel();
this.items = itemModel.GetAllItems();
}
public ObservableCollection<ItemViewModel> Items
{
get
{ return items; }
set
{
items = value;
OnPropertyChanging("Items");
}
}
public ItemViewModel SelectedItem
{
get { return selectedItem; }
set
{
selectedItem = value;
OnPropertyChanging("SelectedItem");
}
}
}
ItemViewModel.cs
的代码public class ItemViewModel:ViewModelBase
{
private int id;
private string itemName;
public int Id
{
get { return id;}
set
{
id = value;
OnPropertyChanging("Id");
}
}
public string ItemName
{
get { return itemName; }
set
{
itemName = value;
OnPropertyChanging("ItemName");
}
}
}
ViewModelBase.cs 基本上是在实施INotifyPropertyChanged
和INotifyPropertyChanging
。 ItemModel.cs 从数据库中获取数据。
我在哪里做错了? txtItemName 文本框未填充。