bubye
bubye

Reputation: 89

C# WPF Stackpanel layout

I have created a stackpanel in a treeviewitem. I'm trying to get my picture next to my checkbox and the text next to my picture box but I don't know how to do that. Currently this is happening:

http://img854.imageshack.us/i/naamloosl.png/

This is my code:

ComboBoxItem tempComboItem = comboBox1.SelectedItem as ComboBoxItem;
        CheckBox cbox = new CheckBox();

        StackPanel panel = new StackPanel();
        panel.Width = 260;
        Label labelTitle = new Label();
        Label labelStatus = new Label();
        Image newImage = new Image();



        newImage.Source = new BitmapImage(new Uri(imageTextBox1.Text));
        newImage.Width = 85;
        newImage.Height = 65;

        panel.Children.Add(newImage);

        labelTitle.Content = itemTextBox1.Text;
        panel.Children.Add(labelTitle);



        labelStatus.Content = "Beschikbaar";
        panel.Children.Add(labelStatus);


        labelStatus.Foreground = Brushes.Lime;

        cbox.Content = panel;

        TreeViewItem newChild = new TreeViewItem();
        newChild.Header = cbox;

Can someone help me out.

I want the checkbox and image and text to be horizontal. I can do with: panel.Orientation.

But the two text labels on the right, I want them vertical, one below the other.

How do I do this?

Upvotes: 0

Views: 3070

Answers (3)

A.R.
A.R.

Reputation: 15704

  <DockPanel>
    <CheckBox DockPanel.Dock="Left"/>

    <StackPanel DockPanel.Dock="Right">
      <TextBlock>My Text One</TextBlock>
      <TextBlock>beschikbaar</TextBlock>
    </StackPanel>

    <Image DockPanel.Dock="Left" Source="myImage.png" />
  </DockPanel>

This is a simple way to achieve the effect. From here you can easily adjust the vertical alignments to your liking. You can also easily do this in code, but I would recommend that you stick to XAML for layout based tasks.

Upvotes: 0

Emond
Emond

Reputation: 50712

I'd make the following XAML: (If you need help to turn this into code let me know)

    <CheckBox>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Image Grid.RowSpan="2" 
                   Width="85"
                   Height="65" />
            <TextBlock Grid.Row="0"
                       Grid.Column="1"
                       Text="Title" />
            <TextBlock Grid.Row="1"
                       Grid.Column="1"
                       Text="beschikbaar" />
        </Grid>
    </CheckBox>

Upvotes: 4

Grant Thomas
Grant Thomas

Reputation: 45058

Do you simply mean you want Horizontal orientation?

If so, try the following:

panel.Orientation = Orientation.Horizontal;

Though, I fear you actually may require more intervention than just setting this property; lets see.

Upvotes: 0

Related Questions