0

I have winform hosting WPF control text block where the contents of text block is dynamic. I want to set the height of the text block based on content. mean while text block is hosted in windows usercontrol (TitleBarHost) and in turn this windows control is dynamically added in another winform at run time. Below is piece of code where control is added dynamically.

Control titleBar = new TitleBarHost(titleInfo);                        
(titleBar as ICustomUI).Initialize(_application, extraData);
panelHeader.Controls.Add(titleBar);
titleBar.Dock = DockStyle.Top;

TitleBarView.xaml

<Grid>
    <TextBlock x:Name="txtTitleBar" Text="{Binding Text}" Style="{StaticResource TextBlockStyle}"/>
</Grid>

TitleBarHost.cs

    public void Initialize(object application, object configurationData)
    {
        m_Model = new TitleBarModel(this.titleInfo, application, configurationData);
        m_ViewModel = new TitleBarViewModel(m_Model);

        m_View = new TitleBarView { DataContext = m_ViewModel };

        if (m_ViewModel != null)
        {
            m_View.FormatString(m_ViewModel.Text);
        }

        elementHost1.Child = m_View;
    }

I need to set the text block height dynamic based on content. I tried to use the ActualHeight prop of text block but doesn't work. Also tried using height Auto, doesn't works!

Chethan
  • 298
  • 1
  • 4
  • 17
  • *I tried to use the ActualHeight prop of text block but doesn't work. Also tried using height Auto, doesn't works!*... those are very poor descriptions and do not help us to understand your problem. Your question is also unclear as a WPF `TextBlock` automatically adjusts its size to its content. – Sheridan Mar 05 '15 at 17:43
  • apologies... I mean i tried setting the 'titleBar' height by taking the ActualHeight property of the text box. And to answer to your second question, you are correct. The height of WPF control textblock will set auto, but since this control is hosted in winform usercontrol, its leaving a blank space if the text content is less. Hope i made a clear statement now! – Chethan Mar 05 '15 at 19:27
  • So you want to adjust the size of the *WinForms control*? – Sheridan Mar 06 '15 at 09:02
  • Exactly, and i am trying to assign the ActualHeight of textblock(WPF control) to the height of the "panelHeader" – Chethan Mar 06 '15 at 19:48
  • Where's your `ElementHost` control? – Sheridan Mar 09 '15 at 09:45
  • its in TitleBarHost usercontrol – Chethan Mar 09 '15 at 19:14
  • So is `titleInfo` your WPF `UserControl`? And how did you *use the `ActualHeight` property of the `TextBlock`* when you don't have access to it? Did you mean the `ActualHeight` property of the `UserControl`? – Sheridan Mar 10 '15 at 08:52
  • yes, titleinfo is an object of type 'TitleBarHost' which is UserControl. I was trying to get the actual height of WPF control and assign it to height of usercontrol which didn't work! – Chethan Mar 10 '15 at 14:27
  • Can you get the value from `UserControl.ActualHeight`? – Sheridan Mar 10 '15 at 14:52
  • its always having value 0(zero) – Chethan Mar 11 '15 at 18:15
  • As a test, try hard coding some text in the `TextBlock` and then see if you can get a value from `ActualHeight`. – Sheridan Mar 12 '15 at 10:42
  • Already did that, if you increase the content, only some part of the text is displayed and rest is clipped off! – Chethan Mar 12 '15 at 14:42
  • Perhaps you should take a look at the [Walkthrough: Mapping Properties Using the ElementHost Control](https://msdn.microsoft.com/en-us/library/ms788740(v=vs.110).aspx) page on MSDN. – Sheridan Mar 12 '15 at 14:44

0 Answers0