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

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

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

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

WPF

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

Q&A

解決済

2回答

4561閲覧

C# comboBoxItemを.csにて追加したいです.

Okachan

総合スコア9

C#

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

WPF

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

1グッド

0クリップ

投稿2021/10/23 10:54

編集2021/10/25 16:47

###はじめに
いろいろと無駄なソースや美しくないと思いますのでご了承下さい.
そのうえでこのようにすれば「綺麗なソースになるよ!」などのアドバイスも頂けたら嬉しいです.
最終的には総合的なアプリケーションにする予定なのでプラグイン化(できるかは別として)する予定です.

前提・実現したいこと

C#にてスケジューラーを作っています.
XAMLでは<ComboBoxItem ~~>と書けば追加されるのはわかりますが,ソースファイルでの追加方法が分かりません.
実現したいのはfor文で回した変数iを追加していくプログラムです.

また数が多くなることも想定してBeginUpdateとEndUpdateを使用してみたいと考えておりましたがその2つのメソッドは使えないのでしょうか?
使用可能でしたらそちらの回答でよろしくお願いします.

該当のソースコード

C#

1 private void addbox_ym(object sender, EventArgs e) 2 { 3 for(int i = 1999; i < 2101; i++) 4 { 5 //selectedYM.Items.Add("test" + i.ToString()); 6 } 7 }

全体のソースコード

XAML

1<Window x:Class="TotalApplication.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:TotalApplication" 7 mc:Ignorable="d" 8 Title="スケジュール表" Height="450" Width="800" MinHeight="225" MinWidth="500"> 9 <Grid> 10 <Grid.RowDefinitions> 11 <RowDefinition Height="24*"/> 12 <RowDefinition Height="395*"/> 13 </Grid.RowDefinitions> 14 <GroupBox x:Name="CalendarGroup1" Header="" Margin="10,16.216,10,10" Grid.Row="1"> 15 <Grid x:Name="CalenderGrid"> 16 <Grid.ColumnDefinitions> 17 <ColumnDefinition Width="1*"/> 18 <ColumnDefinition Width="1*"/> 19 <ColumnDefinition Width="1*"/> 20 <ColumnDefinition Width="1*"/> 21 <ColumnDefinition Width="1*"/> 22 <ColumnDefinition Width="1*"/> 23 <ColumnDefinition Width="1*"/> 24 </Grid.ColumnDefinitions> 25 <Grid.RowDefinitions> 26 <RowDefinition Height="20"/> 27 <RowDefinition Height="15"/> 28 <RowDefinition Height="1*"/> 29 <RowDefinition Height="15"/> 30 <RowDefinition Height="1*"/> 31 <RowDefinition Height="15"/> 32 <RowDefinition Height="1*"/> 33 <RowDefinition Height="15"/> 34 <RowDefinition Height="1*"/> 35 <RowDefinition Height="15"/> 36 <RowDefinition Height="1*"/> 37 <RowDefinition Height="15"/> 38 <RowDefinition Height="1*"/> 39 </Grid.RowDefinitions> 40 <Rectangle Stroke="Black" StrokeThickness="1" Grid.ColumnSpan="7" Grid.RowSpan="13"/> 41 <Rectangle Fill="#ff8000" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" Margin="1 1 0 0" Panel.ZIndex="0"/> 42 <!--5行略--> 43 <Rectangle Fill="#ff8000" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="6" VerticalAlignment="Stretch" Margin="1 1 1 0" Panel.ZIndex="0"/> 44 45 <Rectangle Fill="#d3d3d3" HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0" VerticalAlignment="Stretch" Margin="1 1 0 0" Panel.ZIndex="0"/> 46 <!--47行略--> 47 <Rectangle Fill="#d3d3d3" HorizontalAlignment="Stretch" Grid.Row="11" Grid.Column="6" VerticalAlignment="Stretch" Margin="1 1 0 0" Panel.ZIndex="0"/> 48 49 <Label Content="日" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" FontSize="11" Padding="0"/> 50 <!--曜日略--> 51 <Label Content="土" HorizontalAlignment="Center" Grid.Row="0" Grid.Column="6" VerticalAlignment="Center" FontSize="11" Padding="0"/> 52 53 </Grid> 54 </GroupBox> 55 <Label Content="選択した年月: " HorizontalAlignment="Left" Margin="10,15,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/> 56 <Label x:Name="today" Content="今日の日付: " HorizontalAlignment="Left" Margin="250,15,0,0" VerticalAlignment="Top" Grid.RowSpan="2"/> 57 <ComboBox x:Name="selectedYM" HorizontalAlignment="Left" Margin="95,15,0,0" VerticalAlignment="Top" Width="60" Grid.RowSpan="2"> 58 </ComboBox> 59 </Grid> 60</Window> 61

C#

1using System; 2using System.Windows; 3using System.Windows.Controls; 4using System.Windows.Input; 5using System.Windows.Media; 6using System.Windows.Shapes; 7 8namespace TotalApplication 9{ 10 /// <summary> 11 /// MainWindow.xaml の相互作用ロジック 12 /// </summary> 13 public partial class MainWindow : Window 14 { 15 /// <summary> 16 /// 各宣言の説明 17 /// </summary> 18 /// <param name="sRsC">シングルクリック</param> 19 /// <param name="sRdC">ダブルクリック</param> 20 /// <param name="lastClick">最後にクリックした時間</param> 21 /// <param name="clickTimer">クリック判定用時間</param> 22 /// <param name="dC">ダブルクリックが有効な時True</param> 23 private Rectangle sRsC; 24 //private Rectangle sRdC; 25 //private DateTime lastClick; 26 //private TimeSpan clickTimer; 27 //private bool dC; 28 29 /// <summary> 30 /// MainWindow 31 /// </summary> 32 public MainWindow() 33 { 34 InitializeComponent(); 35 36 CalendarGroup1.Header = String.Format("{0:yyyy年MM月}", DateTime.Now); 37 today.Content = String.Format("今日の日付: {0:yyyy年MM月dd日}", DateTime.Now); 38 39 40 //当月の月初を取得 41 var firstDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); 42 43 //曜日の取得 44 int dayofweek = (int)firstDate.DayOfWeek; 45 46 //月末を取得 47 int lastday = firstDate.AddMonths(1).AddDays(-1).Day; 48 49 50 //月初から月末までを走査 51 for (int day = 1; day <= lastday; day++) 52 { 53 //セル位置 54 int index = (day - 1) + dayofweek; 55 //横位置 56 int x = index % 7; 57 //縦位置 58 int y = index / 7; 59 60 //土日の色変更 61 Color color = Colors.Black; 62 if (x == 0) 63 { 64 color = Colors.Red; 65 } 66 else if (x == 6) 67 { 68 color = Colors.Blue; 69 } 70 71 72 //テキストブロックを生成して日付をグリッドに追加 73 var tb_day = new TextBlock() 74 { 75 Text = string.Format("{0}", day), 76 FontSize = 10, 77 Foreground = new SolidColorBrush(color), 78 Padding = new Thickness(5, 0, 0, 0), 79 HorizontalAlignment = HorizontalAlignment.Left, 80 VerticalAlignment = VerticalAlignment.Top 81 }; 82 this.CalenderGrid.Children.Add(tb_day); 83 tb_day.SetValue(Grid.ColumnProperty, x); 84 tb_day.SetValue(Grid.RowProperty, 1 + 2 * y); 85 86 87 //テキストブロックを生成してメモをグリッドに追加(未実装) 88 var tb_memo = new TextBlock() 89 { 90 Text = string.Format("test"), 91 FontSize = 10, 92 Foreground = new SolidColorBrush(color = Colors.Black), 93 Padding = new Thickness(5, 0, 0, 0), 94 HorizontalAlignment = HorizontalAlignment.Left, 95 VerticalAlignment = VerticalAlignment.Top 96 }; 97 this.CalenderGrid.Children.Add(tb_memo); 98 tb_memo.SetValue(Grid.ColumnProperty, x); 99 tb_memo.SetValue(Grid.RowProperty, 2 + 2 * y); 100 101 //四角形を生成してグリッドに追加 102 //セルの枠線などを表示しイベントをハンドリングする用 103 var rect = new Rectangle(); 104 rect.HorizontalAlignment = HorizontalAlignment.Stretch; 105 rect.VerticalAlignment = VerticalAlignment.Stretch; 106 //背景色 107 rect.Fill = Brushes.Transparent; 108 //枠線調整 109 rect.Margin = (x == 6) ? new Thickness(0.0, -1.0, 0.0, 0.0) : new Thickness(0.0, -1.0, 0.0, 0.0); 110 //イベント設定 111 rect.MouseLeftButtonDown += date_mouseleftbuttondown; 112 this.CalenderGrid.Children.Add(rect); 113 rect.SetValue(Grid.ColumnProperty, x); 114 rect.SetValue(Grid.RowProperty, 2 + 2 * y); 115 } 116 } 117 118 /// <summary> 119 /// セル(日)をクリックした際のイベントハンドラ. 120 /// </summary> 121 /// <param name="sender"></param> 122 /// <param name="e"></param> 123 //選択時の枠組み生成 124 void date_mouseleftbuttondown(object sender, MouseButtonEventArgs e) 125 { 126 //既に選択されたセルがある場合は初期化 127 if (sRsC != null) 128 { 129 sRsC.StrokeDashArray = null; 130 sRsC.StrokeThickness = 0; 131 } 132 133 //枠線に点線セット 134 Rectangle rec = sender as Rectangle; 135 rec.Stroke = Brushes.Black; 136 DoubleCollection dbc = new DoubleCollection(); 137 dbc.Add(1); 138 rec.StrokeDashArray = dbc; 139 rec.StrokeThickness = 1; 140 141 142 //選択セルの保持 143 sRsC = rec; 144 145 } 146 147 private void addbox_ym(object sender, EventArgs e) 148 { 149 for(int i = 1999; i < 2101; i++) 150 { 151 //selectedYM.Items.Add("test" + i.ToString()); 152 } 153 } 154 155 } 156} 157

試したこと

この問題を解決するために様々なサイトを見てきましたがたいていの場合,Buttonのクリックイベントに反応するように作られているの多くわかりませんでした.

###今後の予定
・クリックしたときに枠線が日付部分まで囲われないので修正できたら修正したいと思います.
・年月別々のcomboBoxを作り指定された年月のカレンダーを表示させる予定です.
・追加機能としてはダブルクリックした後にメモが書けるようにしたいと考えています.

補足情報(FW/ツールのバージョンなど)

Windows10
Visual Studio 2019 16.2.5
WPFアプリ
.NET Framework 4.7.2

TN8001👍を押しています

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2021/10/23 11:28

何を作っているかと開発環境ぐらいは書きませんか?
Okachan

2021/10/23 12:33

ご連絡ありがとうございます. 補足情報にVisualStudio2019, 前提・実現したいことに何を作っているかなどの記載をしておりますが, 何が足りないのか教えていただけないでしょうか? ご連絡お願い致します.
退会済みユーザー

退会済みユーザー

2021/10/23 13:10

何を作っているか? ⇒ WinForms, WPF, ASP.NET Web Forms, その他いろいろあるが何か 開発環境 ⇒ OS は何か、.NET か Core のどっちかとそのバージョン
Okachan

2021/10/23 15:04

例などを挙げてくださりありがとうございます. 現在,WPFアプリ(.NET Framework)を使って開発しており,アプリケーションのプロパティでは, .NET Framework 4.7.2 出力の種類:Windowsアプリケーション アセンブリバージョンは1.0.0.0です. これを編集し直せばよろしいでしょうか? いまだ使い慣れておらずお手数おかけしますがご回答よろしくお願いします.
gentaro

2021/10/24 00:22

どうやったらMethodを「メゾッド」と間違えるんだろう。こういう人多いけど。 thだったら「ソ」でしょう。zの音はどっから拾ってるんだ。
退会済みユーザー

退会済みユーザー

2021/10/24 00:38 編集

OS も書いてください。Windows 10?  過去に、話を進めていって、どうも話が合わないと思っていたら、実は Mac (Visual Studio も Mac 用)だったということも ありましたので。また、追加の情報は質問欄を編集して追記願います。(例: Windows 10, Visual Studio 2019 16.11.5, WPF アプリ、.NET Framework 4.7.2) > BeginUpdateとEndUpdateを使用してみたい Windows Forms アプリ用の ComboBox と混同してませんか? WPF 用にはそういうメソッドは無いようですが。 https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.controls.combobox?view=netframework-4.7.2 もし混同していて、質問内容に矛盾があるようでしたら、質問を見直してください。
TN8001

2021/10/24 01:08

WPF タグがありますので追加してください^^
fana

2021/10/24 01:17

> どうやったらMethodを「メゾッド」と間違えるんだろう 犯人は「リゾット」説: 初見時に "Method" という表記ではなく「メソッド」というカタカナ表記に出会う + その瞬間に既存の「リゾット」という単語と頭の中で混じる = 結果として「メソット」とか「メゾッド」とか認識する
guest

回答2

0


UniformGridについては検討してみようと思います.

UniformGridの使い方例として、作ってみたものをあげておきます。

xml

1<Window 2 x:Class="Questions365868.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Title="スケジュール表" 6 Width="800" 7 Height="450" 8 MinWidth="500" 9 MinHeight="225"> 10 <DockPanel Margin="10"> 11 12 <Grid DockPanel.Dock="Top"> 13 <StackPanel Orientation="Horizontal"> 14 <Label Content="選択した年月: " /> 15 <ComboBox 16 x:Name="selectedYear" 17 ItemStringFormat="{}{0}年" 18 SelectionChanged="SelectionChanged" /> 19 <!-- どちらを変えてもやること(セルの入れ替え)は同じなので同一ハンドラで --> 20 <ComboBox 21 x:Name="selectedMonth" 22 ItemStringFormat="{}{0}月" 23 SelectionChanged="SelectionChanged" /> 24 </StackPanel> 25 <Label 26 x:Name="today" 27 HorizontalAlignment="Center" 28 Content="今日の日付: " /> 29 </Grid> 30 31 <GroupBox x:Name="calendarGroup" Header="yyyy年MM月"> 32 <Border BorderBrush="Black" BorderThickness="1"> 33 <DockPanel> 34 <UniformGrid Columns="7" DockPanel.Dock="Top"> 35 <UniformGrid.Resources> 36 <!-- UniformGridの子孫のLabelに自動適用 --> 37 <Style TargetType="Label"> 38 <Setter Property="Background" Value="#ff8000" /> 39 <Setter Property="FontSize" Value="11" /> 40 <Setter Property="HorizontalContentAlignment" Value="Center" /> 41 <Setter Property="Margin" Value="1,0,0,0" /> 42 <Setter Property="Padding" Value="0" /> 43 </Style> 44 </UniformGrid.Resources> 45 <Label Margin="0" Content="" /> 46 <Label Content="" /> 47 <Label Content="" /> 48 <Label Content="" /> 49 <Label Content="" /> 50 <Label Content="" /> 51 <Label Content="" /> 52 </UniformGrid> 53 54 <UniformGrid 55 x:Name="calenderGrid" 56 Background="#d3d3d3" 57 Columns="7" 58 PreviewMouseLeftButtonDown="LeftButtonDown" /> 59 <!-- 各セルにイベントをつけるのは面倒なのでUniformGridでまとめてハンドル --> 60 </DockPanel> 61 </Border> 62 </GroupBox> 63 </DockPanel> 64</Window>

cs

1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Windows; 5using System.Windows.Controls; 6using System.Windows.Input; 7using System.Windows.Media; 8using System.Windows.Shapes; 9 10namespace Questions365868 11{ 12 public partial class MainWindow : Window 13 { 14 private readonly Dictionary<DateTime, string> memos = new Dictionary<DateTime, string> 15 { 16 { new DateTime(2021, 10, 8), "Windows11 Release🐱‍💻" }, 17 { new DateTime(2021, 11, 9), ".NET 6 launch!🎉" }, 18 { DateTime.Now.Date, "test" }, 19 }; 20 21 private Rectangle currentRect; // 選択中(枠の表示中)のRectangle 22 private TextBox currentTextBox; // 編集中のTextBox 23 24 public MainWindow() 25 { 26 InitializeComponent(); 27 28 var d = DateTime.Now.Date; 29 today.Content = $"今日の日付: {d:yyyy年MM月dd日}"; 30 31 selectedYear.ItemsSource = Enumerable.Range(1999, 102); // 1999~2100 32 selectedYear.SelectedItem = d.Year; 33 34 selectedMonth.ItemsSource = Enumerable.Range(1, 12); 35 selectedMonth.SelectedItem = d.Month; 36 } 37 38 private void SelectionChanged(object sender, SelectionChangedEventArgs e) 39 { 40 if (selectedYear.SelectedItem == null || selectedMonth.SelectedItem == null) return; 41 42 calenderGrid.Children.Clear(); 43 44 var year = (int)selectedYear.SelectedItem; 45 var month = (int)selectedMonth.SelectedItem; 46 47 var d = new DateTime(year, month, 1); // 選択年月のついたち 48 calendarGroup.Header = $"{d:yyyy年MM月}"; 49 calenderGrid.FirstColumn = (int)d.DayOfWeek; // ついたちの位置をオフセット 50 51 while (month == d.Month) // 月が替わるまでループ 52 { 53 AddCell(d); // UniformGridに1日分のセルを追加 54 d = d.AddDays(1); // 次の日 55 } 56 } 57 58 private void AddCell(DateTime date) 59 { 60 var grid = new Grid { Background = Brushes.White, }; // セルをまとめるGrid UniformGridのグレーを隠すため白 61 grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(), }); 62 grid.RowDefinitions.Add(new RowDefinition()); 63 64 var textBlock = new TextBlock // 日付表示 65 { 66 FontSize = 10, 67 Foreground = date.DayOfWeek == DayOfWeek.Sunday ? Brushes.Red 68 : date.DayOfWeek == DayOfWeek.Saturday ? Brushes.Blue 69 : Brushes.Black, 70 Padding = new Thickness(5, 0, 0, 0), 71 Text = $"{date.Day}", 72 }; 73 grid.Children.Add(textBlock); 74 75 var textBox = new TextBox // メモ欄 76 { 77 BorderThickness = new Thickness(), 78 FontSize = 10, 79 Padding = new Thickness(5, 0, 0, 0), 80 Text = memos.ContainsKey(date) ? memos[date] : "", // メモがあったら設定 81 TextWrapping = TextWrapping.Wrap, 82 }; 83 textBox.TextChanged += (s, e) => memos[date] = textBox.Text; // TextBoxに変更があれば都度メモも変更 84 Grid.SetRow(textBox, 1); 85 grid.Children.Add(textBox); 86 87 var rectangle = new Rectangle // 枠 88 { 89 Fill = Brushes.Transparent, 90 Stroke = Brushes.Black, 91 StrokeDashArray = new DoubleCollection { 1, }, 92 StrokeThickness = 0, 93 Tag = textBox, // TextBoxをTagに保存(セコいがLeftButtonDownで欲しいので^^; 94 }; 95 Grid.SetRowSpan(rectangle, 2); 96 grid.Children.Add(rectangle); 97 98 calenderGrid.Children.Add(grid); 99 } 100 101 private void LeftButtonDown(object sender, MouseButtonEventArgs e) 102 { 103 if (e.Source is Rectangle rectangle) // Transparentならばここにくる nullだとこない(つまりTextBoxの編集の邪魔にならないということ) 104 { 105 var textBox = rectangle.Tag as TextBox; // Tagに保存したTextBoxを取得 106 if (e.ClickCount == 1) // クリック 107 { 108 if (currentRect != null && currentRect != rectangle) // ほかのセルに枠表示があれば...表示を消す 109 { 110 currentRect.StrokeThickness = 0; // 枠の非表示 111 currentRect.Fill = Brushes.Transparent; // 編集中ならnullになっているので戻す 112 } 113 currentRect = rectangle; // カレントに設定 114 currentRect.StrokeThickness = 1; // 枠の表示 115 116 if (currentTextBox != null && currentTextBox != textBox) // ほかのセルを編集中なら...編集終了 117 { 118 currentTextBox.Select(0, 0); // 文字選択解除 119 currentTextBox = null; // カレントをクリア 120 Keyboard.ClearFocus(); // キャレットが残るのでフォーカスを外す 121 } 122 } 123 124 if (e.ClickCount == 2) // ダブルクリック 125 { 126 if (currentTextBox != textBox) e.Handled = true; // ダブルクリックをTextBoxに通してしまうと文字が全選択されるのでブロック(そのほうが都合がよければこの行はいらない) 127 currentRect.Fill = null; // nullにしてRectangleの当たり判定を消す(TextBoxの編集可) 128 currentTextBox = textBox; // カレントに設定 129 currentTextBox.Focus(); // すぐ編集できるようにフォーカス 130 } 131 } 132 } 133 } 134}

アプリ画像


余裕があればItemsControlBindingなどを調べてもらうと、よりWPFっぽい作りになると思います^^
MVVM - 【WPF】Viewへのコントロールの追加|teratail

投稿2021/10/25 21:42

編集2023/07/29 07:36
TN8001

総合スコア9309

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

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

Okachan

2021/10/26 09:32

TN8001 様 作成したプログラムを載せていただきありがとうございます. ItemsControlやBindingについては前回の回答されてくださったときに調べてみました.その時に思い付いたのが, https://atmarkit.itmedia.co.jp/ait/articles/1711/08/news018.html の記事を参考にしつつ, 月初から月末までを走査したときに,Dictionaryコレクション用keyの生成 ほにゃららして何かにバインドする みたいな考えがありました.(詳しく調べたりしていないので浅い知識ですみません.) MVVM - 【WPF】Viewへのコントロールの追加 を後で詳しく見て調べて作成していきたいと思います.
guest

0

ベストアンサー

XAMLでは<ComboBoxItem ~~>と書けば追加されるのはわかりますが,ソースファイルでの追加方法が分かりません.

この問題を解決するために様々なサイトを見てきましたがたいていの場合,Buttonのクリックイベントに反応するように作られているの多くわかりませんでした.

selectedYM.Items.Add("test" + i.ToString());で入ります。
CalendarGroup1.Headerを設定するのと同じように、MainWindowのコンストラクタででもやればいいだけですが難しいですか?(それ以外の部分のほうがよっぽど難しく見えますが^^;

また数が多くなることも想定してBeginUpdateとEndUpdateを使用してみたいと考えておりましたがその2つのメゾッドは使えないのでしょうか?

ComboBox.BeginUpdate メソッド (System.Windows.Forms) | Microsoft Docs
とかのことでしょうか?
であれば見た通りWinForms用ですのでWPFでは使えません。

一気に割り当てたい場合は、ItemsSourceに入れるのが一般的かと思います。
selectedYM.ItemsSource = Enumerable.Range(1999, 102);

ItemsControl.ItemsSource プロパティ (System.Windows.Controls) | Microsoft Docs


いろいろと無駄なソースや美しくないと思いますのでご了承下さい.
そのうえでこのようにすれば「綺麗なソースになるよ!」などのアドバイスも頂けたら嬉しいです.

個人的にはこっちが本題なのですが^^;

カレンダーにはUniformGridがおすすめです。
UniformGrid クラス (System.Windows.Controls.Primitives) | Microsoft Docs

軽く作ってみた感じのポイント(私がそう思っただけで、「そうしろ!」と言っているわけではありません)

  • セルはTextBlock(日付)・TextBox(メモ)・Rectangle(枠)3つをGridでまとめる
    UniformGridを使う場合はそうせざるを得ない。
  • イベントはUniformGridPreviewMouseLeftButtonDownを使う
    e.SourceRectangleは取れるし、e.ClickCountで(厳密には同じではないが)クリック・ダブルクリック判定もできる。
  • Rectangle.FillTransparentnullを使い分ける
    nullにすると貫通するので、後ろにあるTextBoxの編集可不可を制御しやすい。
  • メモはDictionary<DateTime, string>
    何も考えずtextBox.TextChanged += (s, e) => memos[date] = textBox.Text;と、できるので楽。

RoutedEventArgs.Source プロパティ (System.Windows) | Microsoft Docs

MouseButtonEventArgs.ClickCount プロパティ (System.Windows.Input) | Microsoft Docs

投稿2021/10/24 01:06

TN8001

総合スコア9309

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

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

Okachan

2021/10/25 19:27

TN8001 様 ご回答,ありがとうございます. MainWindow内に以下を直接入れ込みました. selectedYear.ItemsSource = Enumerable.Range(1999, 102).Select(x => String.Format("{0}年", x)); Enumerable.Range が使えない! となりましたが using System.Linq; がないだけでした. UniformGridについては検討してみようと思います.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問