简体   繁体   中英

Bind to Parent.Parent from DataGridTextColumn.Header

hi normally it should work automatically but it doesn't

XAML:

 <DataGrid AutoGenerateColumns="False" Width="350" IsReadOnly="True" 
                              ItemsSource="{Binding StichwortList}" >
           <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding DisplayName}" Width="350">
                      <DataGridTextColumn.Header>
                            <DockPanel Margin="0" Width="340" HorizontalAlignment="Stretch" LastChildFill="True">
                                  <TextBlock DockPanel.Dock="Left" Margin="0" Text="Name"/>
                                  <Button DockPanel.Dock="Right" Content="-" Margin="0" Width="23" Command="{Binding RemoveStichwortCommand}"/>
                                  <Button DockPanel.Dock="Right" Content="+" Margin="0" Width="23" Command="{Binding AddStichwortCommand}"/>
                                  <Label />
                            </DockPanel>
                      </DataGridTextColumn.Header>
                 </DataGridTextColumn>
           </DataGrid.Columns>
  </DataGrid>

relevant Code:

        private RelayCommand _addStichwortCommand;
        public ICommand AddStichwortCommand
        {
            get { return _addStichwortCommand ?? (_addStichwortCommand = new RelayCommand(param => this.OnAddStichwort())); }
        }

        private void OnAddStichwort()
        {
            // some code
        }

        private RelayCommand _removeStichwortCommand;
        public ICommand RemoveStichwortCommand
        {
            get { return _removeStichwortCommand ?? (_removeStichwortCommand = new RelayCommand(param => this.OnRemoveStichwort())); }
        }
private void OnRemoveStichwort()
        {
           // some code
        }

my other bindings to this view works and my Command Binding also works fine normally

so could please anyone tell me whats wrong with my binding?

Edit

ohh i forgot

it is all in a Usercontrol whose Datacontext contains al the Propertys andfor the Bindings and Commands

Soultion:

the DockPanel need to Bind his Datacontext to the Usercontrol Datacontext and the Usercontrol needs a name

x:Name="uc"

<DockPanel Margin="0" Width="340" HorizontalAlignment="Stretch" LastChildFill="True" DataContext="{Binding ElementName=uc, Path=DataContext}">
     <TextBlock DockPanel.Dock="Left" Margin="0" Text="Name"/>
     <Button DockPanel.Dock="Right" Content="-" Margin="0" Width="23" Command="{Binding RemoveStichwortCommand}"/>
      <Button DockPanel.Dock="Right" Content="+" Margin="0" Width="23" Command="{Binding AddStichwortCommand}"/>
      <Label />
</DockPanel>

if the solution above doesn't work you should take a look on this answer

You have to name your UserControl in XAML and use it in binding. Something like following code:

<UserControl x:Name="uc" >
.
.
.
<Button DockPanel.Dock="Right" Content="-" Margin="0" Width="23" Command="{Binding RemoveStichwortCommand, ElementName=uc}"/>
<Button DockPanel.Dock="Right" Content="+" Margin="0" Width="23" Command="{Binding AddStichwortCommand, ElementName=uc}"/>

Hope this help.

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