简体   繁体   中英

Adding reference to TextBox manually in WPF

In Following WPF Code

//class level Variables
public TextBox _txtDef = new TextBox(); 
public TextBox _txtComment = new TextBox();

//Events
private void OnCommentsMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    _txtComment = sender as TextBox;
}

private void OnDefinitionMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    _txtDef = sender as TextBox;
}

Markup

<WPFtoolkit:DataGridTemplateColumn x:Name="dgDefinition" Header="Definition" 
                                           Visibility="Collapsed" Width="300">
  <WPFtoolkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Path=Definition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </DataTemplate>
  </WPFtoolkit:DataGridTemplateColumn.CellTemplate>
  <WPFtoolkit:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <TextBox x:Name="txtDefinition" 
               Text="{Binding Path=Definition, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
               BorderThickness="0" ContextMenuOpening="DefinitionContextMenuOpen"
               MouseDoubleClick="OnDefinitionMouseDoubleClick">
      </TextBox>
    </DataTemplate>
  </WPFtoolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFtoolkit:DataGridTemplateColumn>

<WPFtoolkit:DataGridTemplateColumn x:Name="dgComment" Header="Comment" Width="200"
                                   Visibility="Collapsed">
  <WPFtoolkit:DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding Path=Comment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    </DataTemplate>
  </WPFtoolkit:DataGridTemplateColumn.CellTemplate>
  <WPFtoolkit:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
      <TextBox x:Name="txtComment" 
               Text="{Binding Path=Comment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
               BorderThickness="0" MouseDoubleClick="OnCommentsMouseDoubleClick">
      </TextBox>
    </DataTemplate>
  </WPFtoolkit:DataGridTemplateColumn.CellEditingTemplate>
</WPFtoolkit:DataGridTemplateColumn>   

When I m clicking on a particular cell one of the two event mentioned above get fired and hence only the reference of that particular textbox get added whose event get fired.

Is there any way to add the reference of both textbox while any of the two event get fired.

Thanks

Unfortunately not (IMO). You wrote a DataTemplate. This will instanciated for each entry in your grid. When the event is fired, you will get the TextBox as sender the event was fired for (as you already mentioned). But I don't see any chance to get the instance of the other TextBox (it may not be instanciated already)

Ok,

Firstly an answer, I think that this article:

http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx

may well give you what you need. But...

Why do you need to get both text boxes? I'd like to understand the code structure a bit more because what I see of it feels wrong.

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