Home » WPFRSS

How to bind a windows control in WPF????

Hello Experts,

                 I'm using WPF 4.0 ...I have a WPF Datagrid .... One of my column in a datagrid is a template column..In that template column I have used Win forms Textbox...And my problem is that,

(1) How to bind that win forms texbox control in WPF????

(2) How to access that control or column in Code behind (c#)?

Thanks in Advance

 

 

3 Answers Found

 

Answer 1

hi,

Please go through below link that might be help.

http://nayyeri.net/host-windows-forms-controls-in-wpf

 

Thanks

Nikunj Padaliya

 

Answer 2

Thank u for ur reply...

    But I want to access  a template column  within a Datagrid...I have posted my datagrid  xaml...Please go through it...

How to access that CombinedTextBox in code  behind???

My xaml as follows

<DataGrid Name="grd_bpdata" >
     <DataGrid.Columns>

     <DataGridComboBoxColumn x:Name="cbo_actype" Header="Account Type" ItemsSource="{Binding}" 
            DisplayMemberPath="Name" SelectedValuePath="Id" 
            SelectedValueBinding="{Binding ac_type}" 		

CellStyle="{StaticResourceSingleClick}" MaxWidth="142.25" MinWidth="142.25" Width="142.25" />

     
     <DataGridTextColumn Header="A/c. Code" Binding="{Binding acct_code,Mode=TwoWay}" MaxWidth="142.25" 

MinWidth="142.25" Width="142.25">
      <DataGridTextColumn.EditingElementStyle>
       <Style TargetType="{x:Type TextBox}">
        <Setter Property="MaxLength" Value="16"/>
       </Style>
      </DataGridTextColumn.EditingElementStyle>
     </DataGridTextColumn>
     

     <DataGridTemplateColumn Header="Debit" MaxWidth="142.25" MinWidth="142.25" Width="142.25">
      <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
        <TextBlock Text="{Binding Path=debit}" />
       </DataTemplate>
      </DataGridTemplateColumn.CellTemplate>
      <DataGridTemplateColumn.CellEditingTemplate >
       <DataTemplate>
         
        <WindowsFormsHost>
         <txt:CombinedTextBox  x:Name="txt_combinedText" InputType="Numeric" MaxLength="15" 

NumericPrecision="12" NumericScaleOnFocus="2" NumericScaleOnLostFocus="2"/>
        </WindowsFormsHost>
        
       </DataTemplate>
      </DataGridTemplateColumn.CellEditingTemplate>
     </DataGridTemplateColumn>
     
    </DataGrid.Columns>
     
   </DataGrid>

 

Thanks in Advance

 

 

 

Answer 3

Hi Dhev,

You need to create a dependency property in the custom wpf  TextBox class for each property of the WinForm TextBox if you want it to bind  it. In short, the custom TextBox should have a dependency property defined. For example, the sample below shows how to create a dependency property for the Text property of WinForm TextBox and how to bind it.
Code:

  public class MyTextBox : WindowsFormsHost 
  {
    System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();

    public MyTextBox()
    {
      this.Child = textBox;
      this.Width = textBox.Width;
      this.Height = textBox.Height;

      InitTextProperty();
    }

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",typeof(string),typeof(MyTextBox),
        new PropertyMetadata("",
          new PropertyChangedCallback((d, e) =>
            {
              var myTextBox = d as MyTextBox;
              if(myTextBox != null && myTextBox.textBox != null)
                myTextBox.textBox.Text = myTextBox.GetValue(e.Property) as string;              
            }), null));

    private void InitTextProperty()
    {
      this.textBox.TextChanged += new EventHandler((sender, e) =>
      {
        this.SetValue(TextProperty, this.textBox.Text);
      });
    }
    public string Text
    {
      get { return this.GetValue(TextProperty) as string; }
      set { this.SetValue(TextProperty,value); }
    }
  }

Xaml:

  <Grid>
    <TextBox Name="txtBlock" Text="sb"/>
    <my:MyTextBox Text="{Binding ElementName=txtBlock, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
  </Grid>

 

Let me know if this does not help.
Aland Li

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter