Joymon

Hi Everybody,

Could anybody please help me to convert this code into csharp code

Thanks in advance.

Joy

<Button Name="btn" Content ="Click" Grid.Column="1">

<Button.Template>

<ControlTemplate>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition/>

<ColumnDefinition/>

</Grid.ColumnDefinitions>

<TextBox Grid.Column="0">Hai</TextBox>

<ComboBox Grid.Column="1"></ComboBox>

</Grid>

</ControlTemplate>

</Button.Template>

</Button>




Re: Windows Presentation Foundation (WPF) Help to convert xaml into csharp

Douglas Stockwell

1. XamlReader.Load(...)

or

2.

Button btn = new Button();
btn.Content = "Click";
Grid.SetColumn(btn, 1);

ControlTemplate template = new ControlTemplate(typeof(Button));
FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
FrameworkElementFactory textBox = new FrameworkElementFactory(typeof(TextBox));
textBox.SetValue(TextBox.TextProperty, "Hai");
textBox.SetValue(Grid.ColumnProperty, 0);
FrameworkElementFactory comboBox = new FrameworkElementFactory(typeof(ComboBox));
comboBox.SetValue(Grid.ColumnProperty, 1);

grid.AppendChild(new FrameworkElementFactory(typeof(ColumnDefinition)));
grid.AppendChild(new FrameworkElementFactory(typeof(ColumnDefinition)));
grid.AppendChild(textBox);
grid.AppendChild(comboBox);
template.VisualTree = grid;
btn.Template = template;




Re: Windows Presentation Foundation (WPF) Help to convert xaml into csharp

JoyK

Hi Douglas,

What about the <ColumnDefinitions> Whether it will automatically add columns into grid.ColumnDefinitions upon line textBox.SetValue(Grid.ColumnProperty, 0);

Thanks

Joy






Re: Windows Presentation Foundation (WPF) Help to convert xaml into csharp

Douglas Stockwell

These statements create the 2 ColumnDefinitions:

grid.AppendChild(new FrameworkElementFactory(typeof(ColumnDefinition)));
grid.AppendChild(new FrameworkElementFactory(typeof(ColumnDefinition)));





Re: Windows Presentation Foundation (WPF) Help to convert xaml into csharp

JoyK

Hi Douglas,

Thanks very much for your quick replies.

Best Regards,

Joy