我有一个自定义项目样式的列表框。这包括文本框和设置按钮。我希望按钮始终在项目的最右侧对齐。我似乎无法让它以这种方式运作。
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="200"/>
<ColumnDefinition Width="25" MinWidth="25" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" IsReadOnly="true" Text="{Binding name}"/>
<Button Grid.Column="2" Height="25" Click="bntConfig_Clicked" ToolTip="Edit config entry for this device"
HorizontalAlignment="Right"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Right">
<Viewbox>
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="appbar_settings" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
<Path Width="38.9239" Height="38.9239" Canvas.Left="18.538" Canvas.Top="18.5381" Stretch="Fill" Fill="#FF383838" Data="F1 M 38,23.5C 38.8643,23.5 39.7109,23.5756 40.5337,23.7206L 42.6275,18.5381L 48.1901,20.787L 46.0964,25.9692C 47.6473,27.0149 48.9851,28.3527 50.0308,29.9036L 55.213,27.8099L 57.4619,33.3725L 52.2794,35.4664C 52.4244,36.2891 52.5,37.1357 52.5,38C 52.5,38.8643 52.4244,39.7109 52.2794,40.5337L 57.4619,42.6275L 55.213,48.1901L 50.0308,46.0964C 49.0795,47.5073 47.8865,48.7418 46.5112,49.7405L 48.7844,54.8462L 43.3041,57.2891L 41.0307,52.1828C 40.0533,52.3906 39.0394,52.5 38,52.5C 37.1357,52.5 36.2891,52.4244 35.4664,52.2794L 33.3725,57.462L 27.8099,55.213L 29.9036,50.0309C 28.3527,48.9851 27.0149,47.6473 25.9691,46.0964L 20.787,48.1901L 18.538,42.6275L 23.7206,40.5336C 23.5756,39.7109 23.5,38.8643 23.5,38C 23.5,37.1357 23.5756,36.2891 23.7206,35.4664L 18.538,33.3725L 20.787,27.8099L 25.9691,29.9036C 26.9205,28.4927 28.1135,27.2582 29.4889,26.2594L 27.2157,21.1537L 32.6959,18.7109L 34.9694,23.8172C 35.9468,23.6094 36.9606,23.5 38,23.5 Z M 38,28C 32.4771,28 28,32.4772 28,38C 28,43.5229 32.4771,48 38,48C 43.5228,48 48,43.5229 48,38C 48,32.4772 43.5228,28 38,28 Z "/>
</Canvas>
</Viewbox>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
看起来像: http://imgur.com/a/w9iQo
较小的图像未被显示,较大的图像被调整大小。在这两个我想要一个列表框右侧的垂直条按钮。
感谢您的帮助。
答案 0 :(得分:1)
您是否尝试过以不同方式确定网格列的尺寸?
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="200"/>
<ColumnDefinition Width="25" MinWidth="25" />
</Grid.ColumnDefinitions>
并且还在扩展内容:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
另请注意,按钮的 Grid.Column 应为1,而不是2。
答案 1 :(得分:0)
问题是ListBox.ItemTemplate未共享。它是逐行应用的。
使用ListView / GridView并将按钮放在另一列中。
答案 2 :(得分:0)
正如cicciorocca所说,你需要将你的网格更改为&#34; *&#34;。 &#34;自动&#34;使其成为内容的最小尺寸,*使其达到最大尺寸(在所有&#34; N *和#34之间共享;以N的比例共享宽度)。
因此,您需要让第一列拉伸整个宽度,最后一列是固定大小:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" MinWidth="200"/>
<ColumnDefinition Width="25" MinWidth="25" />
</Grid.ColumnDefinitions>
然而,正如Paparazzi所说,ListBox.ItemTemplate不是共享的,它默认为左对齐。因此,您调整大小的实际网格占用了最小的空间,因此&#34; *&#34;宽度得到了很多忽略。
解决方案是在列表框中设置HorizontalContentAlignment="Stretch"
。这会将每个ItemTemplate拉伸到最大宽度,然后每个都会尊重&#34; *&#34;宽度并将右栏推到最后。
(编辑,我看到cicciorocca现在做了类似的事情,我认为他的解决方案也应该有效)
另外,如上所述,将Grid.Column="2"
设为Grid.Column="1"