简体   繁体   中英

wpf data binding not working

I have a datagrid. When a row is click on the rowdetails is shown. In the rowdetails I have another datagrid next to the datagrid some buttons. The problem I have is binding the buttons which are in the rowdetails. I click the buttons and nothing happens, I point a break in the code behind for the ExecuteCommandButtRGHTSBoth method and it never gets hit. I don't understand why?

View Model code

    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>

Try to use DataContext of your Window using AncestorType and Mode . Let me show an example:

<Button Content="Take Cash" Command="{Binding DataContext.CommandButtRGHTSCash, 
RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" 
Style="{StaticResource ButtonTemplate}" HorizontalAlignment="Left" 
VerticalAlignment="Center"/>

Update:

The awesome developer @ AntonDanylov has shown me good articles to read:

Regarding DataContext here is a good point to start .

And this article explains possible problem with DataGrid.

StepUp's code seems right.

But I'm not sure about a reason. I mean your command properties are defined in a ViewModel(let's call it VM) where I ASSUME you have the collection(HldLogEQCurr) you use as an ItemSource on the DataGrid. A RowDetail is for 1 element, not many. So there you have 1 element of the above collection as a DataContext. And as the Commands are not defined in those elements it won't find it.

Also, in the Output Window, there should be warnings indicating the Bindings failed and giving you more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM