在堆栈元素代码后面添加间距

发布于 2024-10-21 18:55:27 字数 1115 浏览 6 评论 0原文

我进行了 xml 解析并在堆栈面板中创建了复选框。如何添加堆栈面板的子元素之间的间距?它们似乎在没有适当间距的情况下彼此堆叠,我希望在后面的代码(.cs 文件)中执行此操作。

非常感谢。

编辑1: 抱歉忘记添加我的代码:

我的 XAML:

<TabItem Header ="XML PARSING" Name="Tabitem5" Visibility="Visible">
        <StackPanel Name="stack1" >
            <Button Height="23" Name="XmlappendButton" Width="75" HorizontalAlignment="Right" Click="XmlappendButton_Click">Update</Button>
        </StackPanel>

</TabItem>

我的代码后面:

private void xmlparsing()
{
    XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");
    var abc = from target in xmlDoc.Descendants("target")
              select (string)target.Attribute("if");

    foreach(string target in abc)
    {
        if (target != null && !Dictionarycheck.ContainsKey(target))
        {
            System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox();
            chk.Name = target+"CheckBox";
            chk.Content = target;
            chk.Tag = target;
            stack1.Children.Add(chk);
        }
    }       
}

I did xml parsing and created checkboxes in a stack panel. How do i add spacing between the children elements of the stack panel? They seems to be stacking on each other without proper spacing and I wish to do it in the code behind (.cs file).

Many thanks.

EDIT 1:
Sorry forgot to add my codes:

my XAML:

<TabItem Header ="XML PARSING" Name="Tabitem5" Visibility="Visible">
        <StackPanel Name="stack1" >
            <Button Height="23" Name="XmlappendButton" Width="75" HorizontalAlignment="Right" Click="XmlappendButton_Click">Update</Button>
        </StackPanel>

</TabItem>

my code behind:

private void xmlparsing()
{
    XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");
    var abc = from target in xmlDoc.Descendants("target")
              select (string)target.Attribute("if");

    foreach(string target in abc)
    {
        if (target != null && !Dictionarycheck.ContainsKey(target))
        {
            System.Windows.Controls.CheckBox chk = new System.Windows.Controls.CheckBox();
            chk.Name = target+"CheckBox";
            chk.Content = target;
            chk.Tag = target;
            stack1.Children.Add(chk);
        }
    }       
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

变身佩奇 2024-10-28 18:55:27

为您正在创建的子元素留出边距,

_chk.Margin = new Thickness(0,100,0,0); 

但理想情况下,这在 XAML 中会很好。将 XML 数据作为集合绑定到 ListBox,并提供适当的 DataTemplate,其中包含 CheckBox。执行此操作的正确 WPF 方法如下。
XAML

  <ListBox x:Name="lstBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding}" Tag="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

代码隐藏

XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");    
lstBox.ItemsSource = from target in xmlDoc.Descendants("target") select (string)target.Attribute("if");

Give margin to the child elements you are creating

_chk.Margin = new Thickness(0,100,0,0); 

But ideally would be good this in XAML. Bind your XML data as a collection to a ListBox and provide proper DataTemplate with a CheckBox in it. The proper WPF way of doing this would be as follows.
XAML

  <ListBox x:Name="lstBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox Content="{Binding}" Tag="{Binding}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Code Behind

XDocument xmlDoc = XDocument.Load(@"C:\Build.xml");    
lstBox.ItemsSource = from target in xmlDoc.Descendants("target") select (string)target.Attribute("if");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文