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

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

新規登録して質問してみよう
ただいま回答率
85.48%
WPF

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

Q&A

解決済

2回答

5206閲覧

WPF ListBoxの高さを指定したアイテム数に変更したいです

kikiinu

総合スコア21

WPF

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

0グッド

0クリップ

投稿2020/05/24 01:05

編集2020/05/25 13:04

無事、解決したので画像を載せておきます
イメージ説明
イメージ説明
イメージ説明

<以下、最初の質問です>
WPFにてフォントを選択するウィンドウを作成しています
フォントが変更されてもコントロールが適切な位置・大きさになるように
動的に配置したいのですが現在、ListBoxの高さが希望した大きさに変更しません

希望としましてはListBoxのアイテム数を5個と指定したら5個が収まる大きさ(ListBox.Height)
に動的に変更されることです
6個以上だとスクロールバーが表示したいのです

現在は追加されたアイテム数の分、ListBoxの高さが変更されています

イメージ説明

XAML

1<Window x:Class="WpfApp1.FontDialog" 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:WpfApp1" 7 mc:Ignorable="d" 8 Title="フォントの選択" WindowStyle="SingleBorderWindow" ResizeMode="NoResize" SizeToContent="WidthAndHeight" 9 Loaded="Window_Loaded" d:DesignHeight="361.933" Width="542"> 10 11 <DockPanel Margin="10"> 12 <!-- *********************************************************************** -- 13 <!-- ボタン類 DockPanelの指定方法 グーグルで wpf dock fill と検索 --> 14 <!-- *********************************************************************** --> 15 <Grid DockPanel.Dock="Bottom" Margin="0,10,0,0"> 16 <Grid.ColumnDefinitions> 17 <ColumnDefinition Width="2*"/> 18 <ColumnDefinition Width="1*"/> 19 <ColumnDefinition Width="5"/> 20 <ColumnDefinition Width="1*"/> 21 </Grid.ColumnDefinitions> 22 <Grid.RowDefinitions> 23 <RowDefinition Height="Auto"/> 24 </Grid.RowDefinitions> 25 26 <Button Name="btnOK" Grid.Row="0" Grid.Column="1" IsDefault="True" Content="OK" Click="btnOK_Click" TabIndex="0" /> 27 <Button Name="btnCancel" Grid.Row="0" Grid.Column="3" IsCancel="True" Content="キャンセル" TabIndex="1" /> 28 </Grid> 29 30 <Grid> 31 <Grid.ColumnDefinitions> 32 <ColumnDefinition Width="2.5*"/> 33 <ColumnDefinition Width="5"/> 34 <ColumnDefinition Width="2.5*"/> 35 <ColumnDefinition Width="5"/> 36 <ColumnDefinition Width="1*"/> 37 </Grid.ColumnDefinitions> 38 <Grid.RowDefinitions> 39 <RowDefinition Height="Auto"/> 40 <RowDefinition Height="Auto"/> 41 <RowDefinition/> 42 <RowDefinition Height="Auto"/> 43 <RowDefinition/> 44 <RowDefinition Height="Auto"/> 45 <RowDefinition Height="Auto"/> 46 </Grid.RowDefinitions> 47 48 <!-- *********************************************************************** --> 49 <!-- 1列目 --> 50 <!-- *********************************************************************** --> 51 <Label Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" Target="{Binding ElementName=txtFamilyName}" Content="フォント(_F)" /> 52 <TextBox x:Name="txtFamilyName" Grid.Row="1" Grid.Column="0" FontWeight="Bold" /> 53 <ListBox x:Name="lstFamilyName" Grid.Row="2" Grid.Column="0" Grid.RowSpan="5" SelectionChanged="lstFamilyName_SelectionChanged" > 54 <ListBox.ItemsPanel> 55 <ItemsPanelTemplate> 56 <VirtualizingStackPanel /> 57 </ItemsPanelTemplate> 58 </ListBox.ItemsPanel> 59 60 <ListBox.ItemTemplate> 61 <DataTemplate> 62 <StackPanel Orientation="Horizontal"> 63 <TextBlock VerticalAlignment="Center" Margin="3" Text="{Binding}" /> 64 <TextBlock VerticalAlignment="Center" Text="Sample" FontFamily="{Binding}" /> 65 </StackPanel> 66 </DataTemplate> 67 </ListBox.ItemTemplate> 68 </ListBox> 69 70 <!-- *********************************************************************** --> 71 <!-- 2列目 --> 72 <!-- *********************************************************************** --> 73 74 <!-- *********************************************************************** --> 75 <!-- 3列目 --> 76 <!-- *********************************************************************** --> 77 <Label x:Name="lblTypeface" Grid.Row="0" Grid.Column="2" Target="{Binding ElementName=txtTypeface}" Content="タイプフェース(_Y)" /> 78 <TextBox x:Name="txtTypeface" Grid.Row="1" Grid.Column="2" FontWeight="Bold" /> 79 <ListBox x:Name="lstTypeface" Grid.Row="2" Grid.Column="2" SelectionChanged="lstTypeface_SelectionChanged" /> 80 <Label Grid.Row="3" Grid.Column="2" Content="サンプル(_M)" Target="{Binding ElementName=txtSample}" /> 81 <TextBox Name="txtSample" Grid.Row="4" Grid.Column="2" Grid.ColumnSpan="3" AcceptsReturn="True" TextWrapping="Wrap" ToolTip="テキストの編集可能" /> 82 <Label Grid.Row="5" Grid.Column="2" Content="言語(_L)" Target="{Binding ElementName=cmbLanguage}" /> 83 <ComboBox x:Name="cmbLanguage" Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="3" SelectionChanged="cmbLanguage_SelectionChanged" /> 84 85 <!-- *********************************************************************** --> 86 <!-- 4列目 --> 87 <!-- *********************************************************************** --> 88 89 <!-- *********************************************************************** --> 90 <!-- 5列目 --> 91 <!-- *********************************************************************** --> 92 <Label Grid.Row="0" Grid.Column="4" Target="{Binding ElementName=txtFontSize}" Content="サイズ(_S)" /> 93 <TextBox x:Name="txtFontSize" Grid.Row="1" Grid.Column="4" FontWeight="Bold" /> 94 <ListBox x:Name="lstFontSize" Grid.Row="2" Grid.Column="4" SelectionChanged="lstFontSize_SelectionChanged" /> 95 </Grid> 96 </DockPanel> 97</Window> 98

よろしくお願いいたします。

以下の環境です
Visual Studio Community 2019
.NET Core 3.1
WPF Windowsアプリケーション
c#

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

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

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

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

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

guest

回答2

0

自己解決

Stack Overflowにて
リンク内容
件名 : Listbox - Size to content until Max number of rows is met
というので動きました
こちらで様子を見てみます

投稿2020/05/25 10:07

kikiinu

総合スコア21

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

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

TN8001

2020/05/25 12:22

ちょっと勘違いしてました。5個以下は潰れるんですね。 システムフォントが5個以下になることはありえないので、質問を読めてませんでした^^; lstFamilyName.HeightをlstFamilyName.MaxHeightに変えて検証コードに直しました。 もしかしてフィルター等でフォントの個数が動的に変わる感じですか? だとするとこれだとダメですが^^;(Loaded時に最低1個は入っていないと計算できない 試してはいませんが、Stack OverflowのはScrollChangedが入ってるのは少し重いんじゃないかと思いました。
guest

0

いろいろな方法がありそうですが、ぱっと思いついたのはこんなところです。

非常にバカバカしい^^;

xml

1<ListBox 2 x:Name="dummy" 3 Grid.Row="2" 4 Grid.RowSpan="5"> 5 <TextBlock Margin="3" /> 6 <TextBlock Margin="3" /> 7 <TextBlock Margin="3" /> 8 <TextBlock Margin="3" /> 9 <TextBlock Margin="3" /> 10</ListBox> 11<ListBox 12 x:Name="lstFamilyName" 13 Grid.Row="2" 14 Grid.RowSpan="5" 15 Height="{Binding ActualHeight, ElementName=dummy}"> 16</ListBox>

追記
HeightでなくMaxHeightにすべきだった。

xml

1<Window 2 x:Class="Questions264300.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:local="clr-namespace:Questions264300" 6 Title="フォントの選択" 7 MinWidth="542" 8 ResizeMode="NoResize" 9 SizeToContent="WidthAndHeight" 10 WindowStyle="SingleBorderWindow"> 11 <Window.Resources> 12 <local:Point2PixelConverter x:Key="point2PixelConverter" /> 13 </Window.Resources> 14 15 <DockPanel 16 Margin="10" 17 TextBlock.FontFamily="{Binding SelectedValue, ElementName=lstFamilyName, Mode=TwoWay}" 18 TextBlock.FontSize="{Binding SelectedValue, ElementName=lstFontSize, Mode=TwoWay, Converter={StaticResource point2PixelConverter}}"> 19 <Grid Margin="0,10,0,0" DockPanel.Dock="Bottom"> 20 <Grid.ColumnDefinitions> 21 <ColumnDefinition Width="2*" /> 22 <ColumnDefinition Width="1*" /> 23 <ColumnDefinition Width="5" /> 24 <ColumnDefinition Width="1*" /> 25 </Grid.ColumnDefinitions> 26 27 <Button 28 Grid.Column="1" 29 Content="OK" 30 IsDefault="True" /> 31 <Button 32 Grid.Column="3" 33 Content="キャンセル" 34 IsCancel="True" /> 35 </Grid> 36 37 <Grid> 38 <Grid.ColumnDefinitions> 39 <ColumnDefinition Width="2.5*" /> 40 <ColumnDefinition Width="5" /> 41 <ColumnDefinition Width="2.5*" /> 42 <ColumnDefinition Width="5" /> 43 <ColumnDefinition Width="1*" /> 44 </Grid.ColumnDefinitions> 45 <Grid.RowDefinitions> 46 <RowDefinition Height="Auto" /> 47 <RowDefinition Height="Auto" /> 48 <RowDefinition Height="Auto" /> 49 <RowDefinition Height="Auto" /> 50 <RowDefinition /> 51 <RowDefinition Height="Auto" /> 52 <RowDefinition Height="Auto" /> 53 </Grid.RowDefinitions> 54 55 <Label Content="フォント(_F)" Target="{Binding ElementName=txtFamilyName}" /> 56 <TextBox 57 x:Name="txtFamilyName" 58 Grid.Row="1" 59 FontWeight="Bold" /> 60 61 <ListBox 62 x:Name="dummy" 63 Grid.Row="2" 64 Grid.RowSpan="5" 65 VerticalAlignment="Top" 66 Visibility="Hidden"> 67 <TextBlock Margin="3" /> 68 <TextBlock Margin="3" /> 69 <TextBlock Margin="3" /> 70 <TextBlock Margin="3" /> 71 <TextBlock Margin="3" /> 72 <TextBlock Margin="3" /> 73 <TextBlock Margin="3" /> 74 <TextBlock Margin="3" /> 75 <TextBlock Margin="3" /> 76 <TextBlock Margin="3" /> 77 </ListBox> 78 <ListBox 79 x:Name="lstFamilyName" 80 Grid.Row="2" 81 Grid.RowSpan="5" 82 MaxHeight="{Binding ActualHeight, ElementName=dummy}" 83 ItemsSource="{x:Static local:SystemFontsModel.SystemFonts}"> 84 <ListBox.ItemTemplate> 85 <DataTemplate> 86 <StackPanel Orientation="Horizontal"> 87 <TextBlock 88 Margin="3" 89 VerticalAlignment="Center" 90 Text="{Binding}" /> 91 <TextBlock 92 VerticalAlignment="Center" 93 FontFamily="{Binding}" 94 Text="Sample" /> 95 </StackPanel> 96 </DataTemplate> 97 </ListBox.ItemTemplate> 98 </ListBox> 99 100 <Label 101 Grid.Column="2" 102 Content="タイプフェース(_Y)" 103 Target="{Binding ElementName=txtTypeface}" /> 104 <TextBox 105 x:Name="txtTypeface" 106 Grid.Row="1" 107 Grid.Column="2" 108 FontWeight="Bold" /> 109 <ListBox 110 x:Name="lstTypeface" 111 Grid.Row="2" 112 Grid.Column="2" /> 113 <Label 114 Grid.Row="3" 115 Grid.Column="2" 116 Content="サンプル(_M)" 117 Target="{Binding ElementName=txtSample}" /> 118 <TextBox 119 Name="txtSample" 120 Grid.Row="4" 121 Grid.Column="2" 122 Grid.ColumnSpan="3" 123 AcceptsReturn="True" 124 TextWrapping="Wrap" 125 ToolTip="テキストの編集可能" /> 126 <Label 127 Grid.Row="5" 128 Grid.Column="2" 129 Content="言語(_L)" 130 Target="{Binding ElementName=cmbLanguage}" /> 131 <ComboBox 132 x:Name="cmbLanguage" 133 Grid.Row="6" 134 Grid.Column="2" 135 Grid.ColumnSpan="3" /> 136 137 <Label 138 Grid.Column="4" 139 Content="サイズ(_S)" 140 Target="{Binding ElementName=txtFontSize}" /> 141 <TextBox 142 x:Name="txtFontSize" 143 Grid.Row="1" 144 Grid.Column="4" 145 FontWeight="Bold" /> 146 147 <ListBox 148 x:Name="dummy2" 149 Grid.Row="2" 150 Grid.Column="4" 151 VerticalAlignment="Top" 152 Visibility="Hidden"> 153 <ListBoxItem>0</ListBoxItem> 154 <ListBoxItem>0</ListBoxItem> 155 <ListBoxItem>0</ListBoxItem> 156 <ListBoxItem>0</ListBoxItem> 157 <ListBoxItem>0</ListBoxItem> 158 </ListBox> 159 <ListBox 160 x:Name="lstFontSize" 161 Grid.Row="2" 162 Grid.Column="4" 163 MaxHeight="{Binding ActualHeight, ElementName=dummy2}" 164 ItemsSource="{Binding Sizes}" 165 SelectedIndex="5" /> 166 </Grid> 167 </DockPanel> 168</Window>

cs

1using System; 2using System.Collections.Generic; 3using System.Globalization; 4using System.Linq; 5using System.Windows; 6using System.Windows.Data; 7using System.Windows.Markup; 8using System.Windows.Media; 9 10namespace Questions264300 11{ 12 public partial class MainWindow : Window 13 { 14 public int[] Sizes { get; } = { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72, }; 15 16 public MainWindow() 17 { 18 InitializeComponent(); 19 DataContext = this; 20 } 21 } 22 23 public class SystemFontsModel 24 { 25 private static ICollection<FontFamily> _SystemFonts; 26 public static ICollection<FontFamily> SystemFonts => _SystemFonts ??= GetSystemFonts(); 27 28 private static ICollection<FontFamily> GetSystemFonts() 29 { 30 var systemFonts = new List<FontFamily>(); 31 var jp = XmlLanguage.GetLanguage("ja-jp"); 32 var us = XmlLanguage.GetLanguage("en-us"); 33 foreach (var font in Fonts.SystemFontFamilies) 34 { 35 if (font.FamilyNames.ContainsKey(jp)) systemFonts.Add(new FontFamily(font.FamilyNames[jp])); 36 else if (font.FamilyNames.ContainsKey(us)) systemFonts.Add(new FontFamily(font.FamilyNames[us])); 37 else systemFonts.Add(new FontFamily(font.FamilyNames.First().Value)); 38 } 39 systemFonts.Sort((x, y) => string.Compare(x.Source, y.Source, StringComparison.Ordinal)); 40 return systemFonts; 41 } 42 } 43 44 public class Point2PixelConverter : IValueConverter 45 { 46 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 47 => value is int point ? point * (96.0 / 72.0) : DependencyProperty.UnsetValue; 48 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotImplementedException(); 49 } 50} 51

イメージ説明
イメージ説明


ぶっちゃけこんな(選択したフォントサイズと連動する)ダイアログ絶対イヤですけどね(サンプル欄の意味って。。。)

投稿2020/05/24 02:55

編集2023/07/22 06:54
TN8001

総合スコア9321

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

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

kikiinu

2020/05/25 12:56

回答ありがとうございます お礼の返信をしたつもりだったんですけど反映されてなかったみたいです... 最初に教えていただいた方法を実装してみたのですがフォントサイズを大きく変更すると 指定したアイテム数の高さになりませんでした。 5個と指定したのに、2個、3個しか表示されなくなりました 実はStack Overflowのソースは動いたので内容は詳しく見ていません(笑) ごめんなさい^^;
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問