複数の可能性があります。ビューに結合し、イベント使用して最も簡単:
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string property = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
public int Rows { get; private set; } = 3;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
void ListBox_SizeChanged(object sender, SizeChangedEventArgs e)
{
Rows = ((ListBox)sender).ActualHeight > 700 ? 4 : 3;
OnPropertyChanged(nameof(Rows));
}
}
XAML:
<ListBox SizeChanged="ListBox_SizeChanged">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Rows}" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
<ListBoxItem>4</ListBoxItem>
<ListBoxItem>5</ListBoxItem>
<ListBoxItem>6</ListBoxItem>
<ListBoxItem>7</ListBoxItem>
<ListBoxItem>8</ListBoxItem>
</ListBox>
ListBox
のサイズを大きく超える700
したら、それは3〜4行の切り替え方法を表示するには、ウィンドウのサイズを変更