我的 WPF 应用程序中有一个列表框。我知道如何使用 selectionchanged 事件。但是我正在尝试遵循 MVVM 设计。但是我不确定如何做到这一点。
我已经为一个按钮做了这个但是不确定我是否可以做同样的事情?
<Button Grid.Column="0" Name="buttImport"
Content="Import File"
Command="{Binding CommandButtImport}"
Style="{StaticResource ButtonTemplate}"/>
public class ViewModel : INotifyPropertyChanged
{
// for the button that imports the orders file
public ICommand CommandButtImport { get; set; }
public ViewModel()
{
CommandButtImport = new MyCommands(
ExecuteCommandButtImport,
CanExecuteCommandButtImport);
}
private bool CanExecuteCommandButtImport(object parameter)
{
return true;
}
// import button
private void ExecuteCommandButtImport(object parameter)
{
// some code
}
}
编辑请忽略上面的代码
我已经更改了我的代码,所以在我目前拥有的内容下方重新发布。我有一个奇怪的问题。XAML - 主代码包含我的数据网格的代码。App - XAML 下面的块包含我的大部分应用程序的样式,但只是一个片段。
还
在 XAML 中我的数据网格下方添加的代码行 - 用于测试目的的主代码。
<ListBox ItemsSource="{Binding SelectedItem.DuplicateSecurities, ElementName=dataGridOrders}"
SelectedItem="{Binding SelectedItem.Security, ElementName=dataGridOrders}"/>
我的数据网格加载正常。当我单击一行时,该行会展开以显示证券列表。我遇到的问题是,当我单击一个项目时,在此列表框中没有任何反应。但是,我在数据网格下方添加的用于测试目的的列表框确实有效。例如,我单击其中一项并更新我的行,我的行详细信息中的列表框也被选中。为什么我的 rowdetail 中的列表框不起作用但我的数据网格下方的列表框不起作用,这很奇怪。有任何想法吗?
XAML - 主要代码
<StackPanel>
<!-- The data grid to display orders-->
<DataGrid DataContext="{Binding OrderBlock}"
x:Name="dataGridOrders"
ItemsSource="{Binding Orders}"
Style="{StaticResource DataGridTemplate}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"
RowHeaderStyle="{StaticResource DG_RowHeader}"
RowStyle="{StaticResource DG_Row}"
CellStyle="{StaticResource DG_Cell}"
RowDetailsTemplate="{StaticResource DG_RowDetail}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Silver"
RowHeaderWidth="30"
Margin="25,5,20,15">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Action">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectedValue" Value="{Binding StatusGood}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectedValue" Value="{Binding StatusGood}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Fund" Binding="{Binding Account}" IsReadOnly="True"/>
<DataGridTextColumn Header="Security ID" Binding="{Binding Security.ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="ThinkFolio Security ID" Binding="{Binding ThinkFolioSecurityID}" IsReadOnly="True"/>
<DataGridTextColumn Header="Security Name" Binding="{Binding Security.Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Buy/Sell" Binding="{Binding TransType}" IsReadOnly="True"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding OrderQunatity, StringFormat=\{0:N0\}}" IsReadOnly="False"/>
<DataGridTextColumn Header="Currency" Binding="{Binding BuyCurrency}" IsReadOnly="False"/>
<DataGridTextColumn Header="Manager" Binding="{Binding FundManager}" IsReadOnly="True"/>
<DataGridTextColumn Header="Order Reason" Binding="{Binding OrderReason}" IsReadOnly="True"/>
<DataGridTextColumn Header="Reject Reason" Binding="{Binding RejectReason}" IsReadOnly="True" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<ListBox ItemsSource="{Binding SelectedItem.DuplicateSecurities, ElementName=dataGridOrders}" SelectedItem="{Binding SelectedItem.Security, ElementName=dataGridOrders}"/>
</StackPanel>
应用 XAML
<!-- Row Detail Template for Data Grid -->
<DataTemplate x:Key="DG_RowDetail">
<Grid x:Name="RowDetailGrid"
Margin="5"
HorizontalAlignment="Left">
<Border HorizontalAlignment="Left"
VerticalAlignment="Top"
Width="500"
Height="80"
CornerRadius="5">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
<GradientStop Offset="0" Color="Transparent"/>
<GradientStop Offset="1" Color="Transparent"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="2.5*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.ColumnSpan="3"
Margin="5,0,0,5"
HorizontalAlignment="Left"
FontSize="12"
FontWeight="Bold"
Foreground="Black"
Text="Select Security Identifier">
</TextBlock>
<ListBox Grid.Row="1" Grid.ColumnSpan="3" Name="lbIdentifier" ItemsSource="{Binding DuplicateSecurities}" SelectedItem="{Binding Security}"
SelectionMode="Single" HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="0,2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="{Binding Path=ID}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
<TextBlock Grid.Column="1" Text="{Binding Path=Name}" FontSize="10" HorizontalAlignment="Left" Margin="5,0,0,0"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Border>
</Grid>
</DataTemplate>
视图模型
public class ViewModel : INotifyPropertyChanged
{
public ICommand CommandButtImport { get; set; } // for the button that imports the orders file
public ICommand CommandButtSend { get; set; } // the button where the user sends the orders in our data grid to thinkFolio
public ICommand CommandButtExit { get; set; } // exit application
private QoEMain _QoEManager; // manages the Model
public QoEMain QoEManager { get { return this._QoEManager; } set { _QoEManager = value; } }
private OrderBlocks _orderBlock; // order block - contains all the order information
public OrderBlocks OrderBlock
{
get
{
return this._orderBlock;
}
set
{
this._orderBlock = value;
OnPropertyChanged("OrderBlock");
}
}
}
包含其他类的 OrderBlocks 类
public class OrderBlocks : INotifyPropertyChanged
{
private List<Order> _orders;
[XmlElement("tF_Transactions")]
public List<Order> Orders { get { return _orders; } set { _orders = value; OnPropertyChanged("Orders"); } }
}
订单类
public class Order : INotifyPropertyChanged
{
Security security;
public Security Security
{
get { return security; }
set { security = value; OnPropertyChanged("Security"); }
}
List<Security> duplicateSecurities;
public List<Security> DuplicateSecurities
{
get { return duplicateSecurities; }
set { duplicateSecurities = value; OnPropertyChanged("DuplicateSecurities"); }
}
安全等级
public class Security : INotifyPropertyChanged
{
private string _id;
public string ID
{
get
{
return _id;
}
set
{
_id = value;
OnPropertyChanged("ID");
}
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public Security() { }
public Security(string newID, string newName)
{
ID = newID;
Name = newName;
}
编辑 - 我的代码现在可以使用,请查看下面对我有用的代码片段
<DataGrid Grid.Row="1" Grid.Column="0"
ItemsSource="{Binding SelectedItem.DuplicateSecurities, ElementName=dataGridOrders}"
SelectedItem="{Binding SelectedItem.Security, ElementName=dataGridOrders}">