如何在 WPF 中设置扩展器控件的最小大小?

发布于 2024-07-29 04:56:25 字数 674 浏览 6 评论 0原文

如何设置扩展器以显示它包含的某些内容,即使在折叠状态下也是如此? 我有以下代码片段,任何人都可以指出对此代码的更改吗?

<Window x:Class="UI2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="358" Width="300">
<TabControl>
    <TabItem Header="Buga Buga">
        <StackPanel>
            <Expander ClipToBounds="False">
                <ListBox Name="lstProcesses"
                         MinHeight="60">
                </ListBox>
            </Expander>
        </StackPanel>
    </TabItem>
</TabControl>

谢谢

How can I set expander to show some content it encloses even in collapsed state ? I have the following code snippet, can anyone point changes to this code ?

<Window x:Class="UI2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="358" Width="300">
<TabControl>
    <TabItem Header="Buga Buga">
        <StackPanel>
            <Expander ClipToBounds="False">
                <ListBox Name="lstProcesses"
                         MinHeight="60">
                </ListBox>
            </Expander>
        </StackPanel>
    </TabItem>
</TabControl>

Thanks

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

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

发布评论

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

评论(2

一桥轻雨一伞开 2024-08-05 04:56:25

听起来 Expander 不是您应该在这种情况下使用的控件。 Expander 有一个标题和内容,如下所示:

<Expander Header="Visible all the time">
    <TextBlock Text="Hidden until expanded" />
</Expander>

在我看来,您想要一个有时设置为特定高度而有时不受限制的控件。

我认为您可以通过将 ToggleButton (Expander 在内部也使用)绑定到 ListBoxMaxHeight 属性来实现此目的。

Kaxaml 中尝试类似的操作:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:diag="clr-namespace:System.Diagnostics;assembly=System">

  <Page.Resources>
    <!-- A way of getting some test data in Kaxaml -->
    <ObjectDataProvider x:Key="Processes"
                        MethodName="GetProcesses"
                        ObjectType="{x:Type diag:Process}" />
  </Page.Resources>

  <StackPanel>
    <ToggleButton Name="Expand" Content="Expand" />
    <ListBox Name="lstProcesses" 
             ItemsSource="{Binding Source={StaticResource Processes}}"
             DisplayMemberPath="ProcessName">
      <ListBox.Style>
        <Style TargetType="ListBox">
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=Expand, Path=IsChecked}"
                         Value="False">
              <Setter Property="MaxHeight" Value="60" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </ListBox.Style>
    </ListBox>
  </StackPanel>
</Page>

It doesn't sound like Expander is the control you should be using for this scenario. Expander has a header, and content, like this:

<Expander Header="Visible all the time">
    <TextBlock Text="Hidden until expanded" />
</Expander>

It sounds to me like you want a control that's set to a specific height some of the time, and unrestrained at other times.

I think you could achieve this by binding a ToggleButton (which Expander uses too, internally) to the MaxHeight property of your ListBox.

Try something like this in Kaxaml:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:diag="clr-namespace:System.Diagnostics;assembly=System">

  <Page.Resources>
    <!-- A way of getting some test data in Kaxaml -->
    <ObjectDataProvider x:Key="Processes"
                        MethodName="GetProcesses"
                        ObjectType="{x:Type diag:Process}" />
  </Page.Resources>

  <StackPanel>
    <ToggleButton Name="Expand" Content="Expand" />
    <ListBox Name="lstProcesses" 
             ItemsSource="{Binding Source={StaticResource Processes}}"
             DisplayMemberPath="ProcessName">
      <ListBox.Style>
        <Style TargetType="ListBox">
          <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=Expand, Path=IsChecked}"
                         Value="False">
              <Setter Property="MaxHeight" Value="60" />
            </DataTrigger>
          </Style.Triggers>
        </Style>
      </ListBox.Style>
    </ListBox>
  </StackPanel>
</Page>
梨涡 2024-08-05 04:56:25

以下是如何将折叠文本(标题)添加到扩展器中包含的列表框中所选项目的快速示例:

<Expander ClipToBounds="False">
    <ListBox Name="lstProcesses"
                 MinHeight="60">
    </ListBox>
    <Expander.Header>
        <TextBlock Text="{Binding SelectedItem, ElementName=lstProcesses}"/>
    </Expander.Header>
</Expander>

Here's a quick example of how to the Collapsed text (the Header) to the selected item in the listbox contained within the expander:

<Expander ClipToBounds="False">
    <ListBox Name="lstProcesses"
                 MinHeight="60">
    </ListBox>
    <Expander.Header>
        <TextBlock Text="{Binding SelectedItem, ElementName=lstProcesses}"/>
    </Expander.Header>
</Expander>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文