簡體   English   中英

C#WPF Stackpanel布局

[英]C# WPF Stackpanel layout

我已經在treeviewitem中創建了一個stackpanel。 我正在嘗試將我的圖片放在復選框旁邊,將文本放在圖片框旁邊,但是我不知道該怎么做。 當前正在發生:

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

這是我的代碼:

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;

有人可以幫我嗎。

我希望復選框以及圖像和文本為水平。 我可以使用: panel.Orientation.

但是右邊的兩個文本標簽,我希望它們垂直,一個在另一個下面。

我該怎么做呢?

我將進行以下XAML :(如果您需要幫助將其轉換為代碼,請告訴我)

    <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>

您只是意味着要Horizontal放置嗎?

如果是這樣,請嘗試以下操作:

panel.Orientation = Orientation.Horizontal;

雖然,我擔心您實際上可能需要更多的干預,而不僅僅是設置此屬性。 讓我們來看看。

  <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>

這是達到效果的簡單方法。 在這里,您可以根據自己的喜好輕松調整垂直對齊方式。 您也可以在代碼中輕松地做到這一點,但是我建議您堅持使用XAML進行基於布局的任務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM