我有一个数据网格。单击一行时,将显示rowdetails。在rowdetails中,我在datagrid旁边有一些数据网格。我遇到的问题是绑定rowdetails中的按钮。我点击按钮没有任何反应,我在ExecuteCommandButtRGHTSBoth方法的代码中指出了一个突破,它永远不会被击中。我不明白为什么?
查看型号代码
public ICommand CommandButtRGHTSCash { get; set; }
public ICommand CommandButtRGHTSStock { get; set; }
public ICommand CommandButtRGHTSBoth { get; set; }
Void SetupButtons()
{
CommandButtRGHTSCash = new MyCommands(ExecuteCommandButtRGHTSCash, CanExecuteCommandButtRGHTSCash);
CommandButtRGHTSStock = new MyCommands(ExecuteCommandButtRGHTSStock, CanExecuteCommandButtRGHTSStock);
CommandButtRGHTSBoth = new MyCommands(ExecuteCommandButtRGHTSBoth, CanExecuteCommandButtRGHTSBoth);
}
private bool CanExecuteCommandButtRGHTSBoth(object parameter)
{
return true;
}
private void ExecuteCommandButtRGHTSBoth(object parameter)
{
for (int i = 0; i < SelectedHldLogEq.Funds.Count; i++)
// some code
}
MainWindow.xaml
<DataGrid Grid.Row="1"
ItemsSource="{Binding HldLogEQCurr, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedHldLogEq, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource DataGridTemplate1}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeaderCenter1}"
RowStyle="{StaticResource DG_Row1}"
CellStyle="{StaticResource DG_Cell1}"
RowHeaderStyle="{StaticResource DG_RowHeader1}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
Background="Silver"
Margin="50,50,50,50"
CanUserDeleteRows="False"
CanUserAddRows="False"
RowHeaderWidth="30">
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<Grid x:Name="RowDetailGrid"
Margin="5"
HorizontalAlignment="Left">
<Border HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="250"
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="4*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="400"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0"
Grid.Column="0"
Margin="5,5,5,5"
HorizontalAlignment="Left"
FontSize="12"
FontWeight="Bold"
Foreground="Black"
Text="Select action to take">
</TextBlock>
<DataGrid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"
ItemsSource="{Binding SelectedItem.Funds, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
RowStyle="{StaticResource DG_Row}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"
RowHeaderStyle="{StaticResource DG_RowHeaderNested}"
CellStyle="{StaticResource DG_Cell}"
Background="Silver"
HorizontalGridLinesBrush="LightGray"
VerticalGridLinesBrush="LightGray"
CanUserAddRows="False"
CanUserDeleteRows="False"
Margin="50,5,5,20"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Fund Code" Binding="{Binding Code}" IsReadOnly="True" MinWidth="75"/>
<DataGridTextColumn Header="Fund Code SS" Binding="{Binding CodeSS}" IsReadOnly="True" MinWidth="75"/>
<DataGridTextColumn Header="Number of Rights" Binding="{Binding NewNominal, StringFormat={}{0:N0}}" IsReadOnly="True"/>
<DataGridCheckBoxColumn Header="Take Cash" Binding="{Binding OptionOne, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
</DataGridCheckBoxColumn>
<DataGridCheckBoxColumn Header="Take Stock" Binding="{Binding OptionTwo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Column="2" Grid.Row="0" Grid.RowSpan="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="180"/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1" Grid.Column="0">
<Button Height="40" Width="150" Margin="0,5,0,0" Content="Take Cash"
Command="{Binding CommandButtRGHTSCash}" Style="{StaticResource ButtonTemplate}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Button Height="40" Width="150" Margin="0,25,0,0" Content="Task Stock"
Command="{Binding CommandButtRGHTSStock}" Style="{StaticResource ButtonTemplate}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Button Height="40" Width="150" Margin="0,25,0,0" Content="Take Both"
Command="{Binding CommandButtRGHTSBoth}" Style="{StaticResource ButtonTemplate}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Grid.Row="1" Grid.Column="1">
<TextBlock Grid.Row="1" Grid.Column="1" Margin="0,5,5,5" HorizontalAlignment="Left" FontSize="12" FontWeight="Bold" Foreground="Black"
Text="Select Date Effective From"/>
<DatePicker Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,25,0,0"
BorderThickness="0" SelectedDate="{Binding DateCheck, Mode=TwoWay}" Width="200"/>
</StackPanel>
<Button Grid.Row="1" Grid.Column="2" Height="40" Width="150" Margin="20,5,0,0" Content="Update Database"
Command="{Binding CommandButtRGHTSExUpdate}" Style="{StaticResource ButtonTemplate}" HorizontalAlignment="Left" VerticalAlignment="Top"/>
</Grid>
</Grid>
</Border>
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTextColumn Header="Date Entered" IsReadOnly="True" Binding="{Binding DateEntered, StringFormat={}\{0:dd-MMM-yy\}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="75"/>
<DataGridTextColumn Header="Date Effective" IsReadOnly="True" Binding="{Binding DateEffective, StringFormat={}\{0:dd-MMM-yy\}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="75"/>
<DataGridTextColumn Header="Sedol" IsReadOnly="True" Binding="{Binding Security.Sedol}" MinWidth="75"/>
<DataGridTextColumn Header="Name" IsReadOnly="True" Binding="{Binding Security.Name}" MinWidth="200"/>
<DataGridTextColumn Header="Ratio" IsReadOnly="True" Binding="{Binding RatioNew}" MinWidth="75"/>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:1)
尝试使用DataContext
和Window
使用AncestorType
的{{1}}。让我举个例子:
Mode
<强>更新强>
令人敬畏的开发者@ AntonDanylov向我展示了很好的文章:
关于DataContext,这里是good point to start。
答案 1 :(得分:1)
StepUp的代码似乎是正确的。
但我不确定是有原因的。我的意思是你的命令属性是在ViewModel中定义的(让我们称之为VM)我在ASSUME你有你在DataGrid上用作ItemSource的集合(HldLogEQCurr)。 RowDetail用于1个元素,而不是很多。因此,您将上述集合中的1个元素作为DataContext。由于命令没有在这些元素中定义,因此无法找到它。
此外,在输出窗口中,应该有警告表明绑定失败并为您提供更多信息。