0

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?

2
  • 2
    Why are you doing it this way? Wouldn't it be much easier to use ItemTemplate? Commented Apr 7, 2011 at 11:30
  • Indeed. Use the put the xaml from my answer in a DataTemplate and assign it to the ItemTemplate property of the Treeview (do not forget to using {Binding } instead of hard coded values) Commented Apr 7, 2011 at 12:28

3 Answers 3

4

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>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 layout belongs in the xaml. This is WPF after all, not winforms.
0

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.

2 Comments

no not all needs to be horizontal, the 2 text labels have to be vertical
@anony g:OK, then you'll need more elements within your StackPanel in order to accommodate that - a Grid, for instance; and lay out your elements even more finely in this manner.
0
  <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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.