Hi Team,
I want to dynamically create DataTemplate for PopupButton with multililine text box.
I want to convert below code in code behind. Please have look at following code. Please suggest what will be the value for PopupContent DP?
I want to dynamically create DataTemplate for PopupButton with multililine text box.
I want to convert below code
<Window x:Class="DescriptionField.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"
Title="Window1" Height="300" Width="300">
<Grid x:Name="myGrid">
<shared:PopupButton Height="25" Width="20" DisplayMode="PopupOnly" PopupHorizontalOffset="-15" PopupVerticalOffset="-25" >
<shared:PopupButton.PopupContent>
<TextBox Height="50" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" >This text can be updated as needed and the focus can be moved outside the popup, which will not close until the popup is explicitly closed. Which can be done by clicking the popup indicator again, or clicking the close button above.</TextBox>
</shared:PopupButton.PopupContent>
</shared:PopupButton>
</Grid>
</Window>
private void InitMultiLineTextBox()
{
PropertyGridCategoryItem category = new PropertyGridCategoryItem();
category.DisplayName = "Person Information";
myGrid.Items.Add(category);
PropertyGridPropertyItem item = new PropertyGridPropertyItem();
item.DisplayName = "Name";
item.Value = "Maulik Gordiya";
ApplyMultiLineTextBoxDataTemplate(item);
category.Accessors.Add(item);
}
private void ApplyMultiLineTextBoxDataTemplate(PropertyGridPropertyItem propertyGridItem)
{
try
{
DataTemplate dataTemplate = new DataTemplate(); // DataTemple
FrameworkElementFactory parentFactory = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory childFactoryTxtBox = new FrameworkElementFactory(typeof(TextBox));
childFactoryTxtBox.SetBinding(TextBox.TextProperty, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) }, Mode = BindingMode.TwoWay, ValidatesOnExceptions = true, NotifyOnValidationError = true });
childFactoryTxtBox.SetValue(TextBox.IsReadOnlyProperty, true);
childFactoryTxtBox.SetValue(TextBox.BorderThicknessProperty, new Thickness(0));
childFactoryTxtBox.SetBinding(TextBox.IsEnabledProperty, new Binding("IsEnabled") { Source = propertyGridItem });
childFactoryTxtBox.Name = "childTextBox";
FrameworkElementFactory popupButtonFactory = new FrameworkElementFactory(typeof(PopupButton));
popupButtonFactory.SetValue(PopupButton.DisplayModeProperty, PopupButtonDisplayMode.PopupOnly);
popupButtonFactory.SetValue(PopupButton.WidthProperty, 20.0);
popupButtonFactory.SetValue(PopupButton.PopupHorizontalOffsetProperty, -15.0);
popupButtonFactory.SetValue(PopupButton.PopupVerticalOffsetProperty, -25.0);
popupButtonFactory.Name = "childMultiLineTextBox";
TextBox tb = new TextBox();
tb.TextWrapping = TextWrapping.Wrap;
tb.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
tb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
tb.SetBinding(TextBox.TextProperty, new Binding("Value") { RelativeSource = new RelativeSource() { AncestorType = typeof(IPropertyDataAccessor) } });
popupButtonFactory.SetValue(PopupButton.PopupContentProperty, tb); // Here exception comes. <i><b>HOW CAN I SET PopupContent?</b></i>
parentFactory.AppendChild(childFactoryTxtBox);
parentFactory.AppendChild(popupButtonFactory);
dataTemplate.VisualTree = parentFactory;
propertyGridItem.ValueTemplate = dataTemplate;
}
catch (Exception ex)
{
}
}