【发布时间】:2016-06-29 20:11:17
【问题描述】:
我正在尝试使用 SelectButton (https://gist.github.com/loraderon/580405),但我需要为其指定 MinWidth。否则它的宽度就是扩展器的宽度。删除 ColumnSpan 或设置第一列 Auto 并没有成功。我真的希望它在列表 + 扩展符号中始终具有最宽元素的宽度。
<UserControl x:Class="loraderon.Controls.SelectButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:loraderon.Controls"
mc:Ignorable="d"
SizeChanged="UserControl_SizeChanged"
d:DesignHeight="30" d:DesignWidth="100">
<Grid
x:Name="SplitGrid"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="23" />
</Grid.ColumnDefinitions>
<Button
x:Name="Button"
Click="Button_Click"
Grid.ColumnSpan="2"
Padding="0"
HorizontalContentAlignment="Left"
>
<ContentControl
x:Name="ButtonContent"
HorizontalContentAlignment="Center"
ContentTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemTemplate}"
/>
</Button>
<Expander
x:Name="Expander"
Expanded="Expander_Expanded"
Collapsed="Expander_Collapsed"
Grid.Column="1"
VerticalAlignment="Center"
IsExpanded="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=IsExpanded}"
/>
<Popup
IsOpen="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=IsExpanded}"
PlacementTarget="{Binding ElementName=Button}"
PopupAnimation="Fade"
StaysOpen="False"
>
<ListBox
x:Name="ListBox"
SelectionMode="Single"
SelectionChanged="ListBox_SelectionChanged"
SelectedIndex="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=SelectedIndex, Mode=TwoWay}"
ItemTemplate="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemTemplate}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:SelectButton}}, Path=ItemsSource}"
/>
</Popup>
</Grid>
</UserControl
编辑:我放置控件的窗口有:
SizeToContent="WidthAndHeight"
这导致下面的两个答案都不起作用。将按钮放置在各种控件/容器中时,是否有更强大的解决方案?似乎控件的构建方式不是很健壮。弹出窗口不是可视树的一部分使其成为一个糟糕的选择。
【问题讨论】:
-
使用的时候不能直接设置Width吗?
-
感谢您的回答,但当然不是。我不知道这些项目在不同语言中的使用时间有多长。如果未指定大小,则 plus 控件应正确调整其内容的大小。
-
也许你可以在用户控件和网格中偷偷添加一些水平拉伸属性。
-
我所做的是
但这发生得太晚了。延长一次就可以了,但一开始就不行了 -
你不能将 MinWidth 设置为 ListBox 宽度,如 MinWidth="{Binding Width, ElementName=ListBox}" 并在必要时使用可视状态管理器进行更改,否则我遗漏了什么?