2
나는 이것이 중복 일지 모른다는 것을 알고 있지만, 최선의 해결책을 찾지 못했습니다. ListBox.ItemTemplate을 사용하여 문제가 발생했습니다. 콘텐츠 격자를 HorizontalAlignment = "늘이기"(작동하지 않음)으로 설정합니다. 그래서 Grid의 너비를 ListBoxItem에 바인딩하려했지만 마지막 항목이 이상하게 동작합니다. 너비에 바인딩하면 ListBox, 거기에 스크롤바, 변환기가 그것을 해결할 수 있지만, 나는 거기에 더 간단하고 우아한 해결책이 있어야한다고 생각합니다.ListBoxItem의 너비에 WPF ListBox.ItemTemplate의 내용을 늘리는 가장 좋은 방법
Codebehind가 :
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new List<Data>()
{
new Data("a1","a2"),
new Data("b1","b2"),
new Data("c1","c2")
};
}
public class Data
{
public Data(string s1, string s2)
{
this.S1 = s1;
this.S2 = s2;
}
public string S1 { get; set; }
public string S2 { get; set; }
}
}
XAML :
<Grid>
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Blue"
Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}},
Path=ActualWidth, Mode=OneWay}"
>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding S1, Mode=OneWay}"
FontSize="20" Grid.Column="0"/>
<TextBlock Text="{Binding S2, Mode=OneWay}"
FontSize="20" Grid.Column="1"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
많은 시간과 코드를 절약 할 수 있습니다. –