teratail header banner
teratail header banner
質問するログイン新規登録

質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.30%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

Q&A

解決済

2回答

8453閲覧

WPF ListBoxの表示行数の変更

dn315

総合スコア201

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

WPF

Windows Presentation Foundation (WPF) は、魅力的な外観のユーザー エクスペリエンスを持つ Windows クライアント アプリケーションを作成するための次世代プレゼンテーション システムです

0グッド

0クリップ

投稿2016/09/26 05:20

0

0

###前提・実現したいこと
WPFにてListBoxの表示行数を固定したいと考えております。
ControlTemplateを作成し、UniformGridにて行数を5行に固定したのですが
表示がとても遅く描画がもたついてしまいます。
改善する方法ご御教授いただきたいです。
よろしくお願いします。

###該当のソースコード

XAML

1<Window x:Class="WpfApplication9.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:WpfApplication9" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 10 <Window.Resources> 11 <ControlTemplate x:Key="ListBoxTemplate"> 12 <UniformGrid Rows="5" Columns="1" IsItemsHost="True"/> 13 </ControlTemplate> 14 </Window.Resources> 15 16 <Grid> 17 <ListBox ItemsSource="{Binding}" Template="{StaticResource ListBoxTemplate}"/> 18 </Grid> 19</Window>

C#

1using System.Linq; 2using System.Windows; 3 4namespace WpfApplication9 5{ 6 /// <summary> 7 /// MainWindow.xaml の相互作用ロジック 8 /// </summary> 9 public partial class MainWindow : Window 10 { 11 public MainWindow() 12 { 13 InitializeComponent(); 14 this.DataContext = Enumerable.Range(1, 1000).ToList<int>(); 15 } 16 } 17}

###補足情報(言語/FW/ツール等のバージョンなど)
Microsoft Visual Studio 2015
Visual C# 2015

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

こんにちは。

パフォーマンスの調査はしてませんが、ControlTemplateで置き換えるのではなく、ListBoxのコンテナのテンプレートだけ指定してみたら改善されないでしょうか。

XML

1<Grid> 2 <ListBox ItemsSource="{Binding}"> 3 <ListBox.ItemsPanel> 4 <ItemsPanelTemplate> 5 <UniformGrid Rows="5" Columns="1" IsItemsHost="True" /> 6 </ItemsPanelTemplate> 7 </ListBox.ItemsPanel> 8 </ListBox> 9</Grid>

投稿2016/09/26 06:39

Tak1wa

総合スコア4791

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dn315

2016/09/26 07:49

ご回答ありがとうございます。 だいぶパフォーマンスが改善されました。 表示件数に関しては、あまりスマートな方法ではないですが DataContextの方で制御してみようと思います。 ありがとうございました
guest

0

常に先頭5個しか出さないのであれば、これでいいのでは?(ViewModelでTake(5)すればいいだけな気もしますが^^;

xml

1<ListBox> 2 <ListBox.ItemsPanel> 3 <ItemsPanelTemplate> 4 <UniformGrid Columns="1" /> 5 </ItemsPanelTemplate> 6 </ListBox.ItemsPanel> 7 <ListBoxItem Content="{Binding [0]}" /> 8 <ListBoxItem Content="{Binding [1]}" /> 9 <ListBoxItem Content="{Binding [2]}" /> 10 <ListBoxItem Content="{Binding [3]}" /> 11 <ListBoxItem Content="{Binding [4]}" /> 12</ListBox>

より一般的な「表示行数の固定」(高さによらず常に同じ個数が見える)は、高さから計算するしかなさそうです。

xml

1<Window 2 x:Class="Q49265.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:math="http://hexinnovation.com/math" 6 Width="800" 7 Height="450" 8 ThemeMode="System"> 9 <Window.Resources> 10 <ControlTemplate x:Key="ListBoxTemplate"> 11 <UniformGrid Columns="1" IsItemsHost="True" Rows="5" /> 12 </ControlTemplate> 13 <math:MathConverter x:Key="Math" /> 14 </Window.Resources> 15 16 <DockPanel> 17 <UniformGrid DockPanel.Dock="Top" Rows="1"> 18 <Button HorizontalAlignment="Stretch" Click="Button_Click" Content="ten thousand" /> 19 <Button HorizontalAlignment="Stretch" Click="Button_Click_1" Content="two" /> 20 </UniformGrid> 21 22 <UniformGrid Rows="1"> 23 <!--<ListBox ItemsSource="{Binding}" Template="{StaticResource ListBoxTemplate}"> 24 <ListBox.ItemContainerStyle> 25 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem"> 26 <Setter Property="VerticalContentAlignment" Value="Stretch" /> 27 <Setter Property="HorizontalContentAlignment" Value="Center" /> 28 </Style> 29 </ListBox.ItemContainerStyle> 30 </ListBox>--> 31 32 <ListBox> 33 <ListBox.ItemsPanel> 34 <ItemsPanelTemplate> 35 <UniformGrid Columns="1" /> 36 </ItemsPanelTemplate> 37 </ListBox.ItemsPanel> 38 <!-- 5個以下があり得るなら --> 39 <ListBox.ItemContainerStyle> 40 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem"> 41 <Setter Property="Padding" Value="12,0" /> 42 <Style.Triggers> 43 <Trigger Property="Content" Value="{x:Null}"> 44 <Setter Property="Visibility" Value="Hidden" /> 45 </Trigger> 46 </Style.Triggers> 47 </Style> 48 </ListBox.ItemContainerStyle> 49 <ListBoxItem Content="{Binding [0]}" /> 50 <ListBoxItem Content="{Binding [1]}" /> 51 <ListBoxItem Content="{Binding [2]}" /> 52 <ListBoxItem Content="{Binding [3]}" /> 53 <ListBoxItem Content="{Binding [4]}" /> 54 </ListBox> 55 56 <!-- より一般的な「表示行数の固定」(高さによらず常に同じ個数が見える) --> 57 <ListBox x:Name="listBox" ItemsSource="{Binding}"> 58 <ListBox.ItemContainerStyle> 59 <Style BasedOn="{StaticResource {x:Type ListBoxItem}}" TargetType="ListBoxItem"> 60 <Setter Property="Height" Value="{Binding ActualHeight, ElementName=listBox, ConverterParameter=x/5, Converter={StaticResource Math}}" /> 61 <Setter Property="Padding" Value="12,0" /> 62 </Style> 63 </ListBox.ItemContainerStyle> 64 </ListBox> 65 </UniformGrid> 66 </DockPanel> 67</Window>

cs

1using System.Windows; 2 3namespace Q49265; 4 5public partial class MainWindow : Window 6{ 7 public MainWindow() => InitializeComponent(); 8 9 private void Button_Click(object sender, RoutedEventArgs e) 10 => DataContext = Enumerable.Range(1, 10000); 11 12 private void Button_Click_1(object sender, RoutedEventArgs e) 13 => DataContext = Enumerable.Range(1, 2); 14}

NuGet Gallery | MathConverter

アプリ動画

投稿2024/11/17 13:31

TN8001

総合スコア10108

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問