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

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

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

.NETとは、主に.NET Frameworkと呼ばれるアプリケーションまたは開発環境を指します。CLR(共通言語ランタイム)を搭載し、入力された言語をCIL(共通中間言語)に変換・実行することが可能です。そのため、C#やPythonなど複数の言語を用いることができます。

C#

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

Q&A

解決済

1回答

839閲覧

(WPF)datagridの出力項目の置き換え

mitu1122

総合スコア1

.NET

.NETとは、主に.NET Frameworkと呼ばれるアプリケーションまたは開発環境を指します。CLR(共通言語ランタイム)を搭載し、入力された言語をCIL(共通中間言語)に変換・実行することが可能です。そのため、C#やPythonなど複数の言語を用いることができます。

C#

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

XAML

XAML(Extensible Application Markup Language)はWPF、Silverlight、Windows PhoneそしてWindows Store appsでユーザーインターフェースを定義するために使われるXML言語です。

WPF

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

1グッド

1クリップ

投稿2022/12/06 12:46

現在、.net6(WPF)+prism+ReactivePropertyでプログラムを学習しています。
WPFを初めて勉強していてprism及びReactivePropertyを使うのは初めてです。
データベースから対象データを抽出して、DataTable取得したデータをDataGridのItemSourceにバインドして出力しています。
ここでDataGrid上で色々データを出力している中でDBテーブル上にコードしか持たないカラムをDataGridではコードを表示させず、固定の名称を出力したいと思っています。(コード:1の場合は「可」を表示、コード9は「不可」を表示)
どのような手段があるのか教えていただけませんでしょうか?
ReactivePropertyを使って表示する方法がわかればそちらを使いたいです。
また、MVVMやWPFやPrismやReactivePropertyを勉強するのにいいサイトやサンプルがあれば
教えてください!
よろしくお願いします!

該当のソースコード

MasterWindow.xaml

xaml

1<Window x:Class="SupportSystem.Views.MasterWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:prism="http://prismlibrary.com/" 5 prism:ViewModelLocator.AutoWireViewModel="True" 6 xmlns:vm="clr-namespace:SupportSystem.ViewModels" 7 xmlns:i="http://schemas.microsoft.com/xaml/behaviors" 8 Title="{Binding Title}" Height="768" Width="1024" WindowStartupLocation="CenterScreen"> 9 <Window.DataContext> 10 <vm:MasterWindowViewModel/> 11 </Window.DataContext> 12 <Grid Background="WhiteSmoke" FocusManager.FocusedElement="{Binding ElementName=searchBottun}"> 13 <Grid.RowDefinitions> 14 <RowDefinition Height="30"/> 15 <RowDefinition Height="70.96"/> 16 <RowDefinition/> 17 <RowDefinition Height="30"/> 18 </Grid.RowDefinitions> 19 <Border 20 BorderBrush="CornflowerBlue" 21 BorderThickness="1" 22 CornerRadius="10" 23 HorizontalAlignment="Left" 24 Width="445" 25 Margin="20,21,0,0" RenderTransformOrigin="0.5,0.5" Grid.RowSpan="2"> 26 <StackPanel Orientation="Horizontal" Margin="0,0,-561,0"> 27 <StackPanel HorizontalAlignment="Center" Width="444"> 28 29 <Grid> 30 <Grid.RowDefinitions> 31 <RowDefinition/> 32 </Grid.RowDefinitions> 33 <Grid.ColumnDefinitions> 34 <ColumnDefinition Width="330"/> 35 <ColumnDefinition Width="100"/> 36 </Grid.ColumnDefinitions> 37 <GroupBox 38 Header="コード範囲" 39 Margin="10,2,20,10" 40 Height="50" 41 BorderBrush="CornflowerBlue" 42 BorderThickness="1" Width="300" 43 HorizontalAlignment="Left"> 44 <StackPanel Orientation="Horizontal"> 45 <CheckBox IsChecked="{Binding CheckBoxAll.Value}" Content="全件" Margin="2,5,20,3"/> 46 <TextBox Width="25" Text="{Binding InputSttid.Value}" IsReadOnly="{Binding ReadOnlySttid.Value}" Height="20"/> 47 <Label Content="~"></Label> 48 <TextBox Width="25" Text="{Binding InputEndid.Value}" IsReadOnly="{Binding ReadOnlyEndid.Value}" Height="20"/> 49 </StackPanel> 50 </GroupBox> 51 <Button x:Name="searchButton" Command="{Binding SearchCommand}" Grid.Row="0" Grid.Column="1" Content="検索" Height="23" Width="80" HorizontalAlignment="Right"/> 52 </Grid> 53 54 </StackPanel> 55 <StackPanel> 56 <StackPanel Orientation ="Horizontal"/> 57 </StackPanel> 58 </StackPanel> 59 </Border> 60 <DataGrid Grid.Row="2" 61 AutoGenerateColumns="False" 62 Margin="20,10,15,10" 63 BorderBrush="CornflowerBlue" 64 BorderThickness="1" 65 HeadersVisibility="All" 66 x:Name="mainDataGrid" 67 IsReadOnly="True" 68 SelectionMode="Single" 69 AlternatingRowBackground ="AliceBlue" 70 ItemsSource="{Binding MainDataGrid.Value}" LoadingRow="mainDataGrid_LoadingRow"> 71 <DataGrid.RowHeaderTemplate> 72 <DataTemplate> 73 <Rectangle Width="10" /> 74 </DataTemplate> 75 </DataGrid.RowHeaderTemplate> 76 <DataGrid.Columns> 77 <DataGridTextColumn Header="コード" Width="150" 78 Binding="{Binding id}"/> 79 <DataGridTextColumn Header="種別" Width="150"/> 80 <DataGridTextColumn Header="名称" Width="150" 81 Binding="{Binding name}"/> 82 <DataGridTextColumn Header="数量量" Width="150" 83 Binding="{Binding volume}"/> 84 <DataGridTextColumn Header="表示順" Width="150" 85 Binding="{Binding display_order}"/> 86 </DataGrid.Columns> 87 </DataGrid> 88 <StatusBar Grid.Row="3" Margin="0,5,0,0"/> 89 </Grid> 90</Window> 91 92

MasterWindowViewModel.cs

c#

1using Prism.Commands; 2using Prism.Mvvm; 3using System.Reactive.Linq; 4using Reactive.Bindings; 5using System; 6using System.Collections.Generic; 7using System.Linq; 8using System.Security.Cryptography.X509Certificates; 9using DataAccess; 10using System.Data; 11using NpgSqlDataAccessManager; 12using System.ComponentModel; 13using DepartmentSupportSystem.Views; 14 15namespace SupportSystem.ViewModels 16{ 17 public class MasterWindowViewModel : BindableBase, INotifyPropertyChanged 18 { 19 private string _title = "マスタ登録"; 20 public string Title 21 { 22 get { return _title; } 23 set { SetProperty(ref _title, value); } 24 } 25 26 public ReactiveProperty<string> InputSttid { get; } 27 public ReactiveProperty<string> InputEndid { get; } 28 public ReactiveProperty<bool> CheckBoxAll { get; } 29 30 public ReactiveCommand SearchCommand { get; } 31 32 public ReactiveProperty<DataTable> MainDataGrid { get; } 33 34 public ReactiveCommand ShowDataEditWindow { get; } 35 public ReactiveCommand ShowDataRegistWindow { get; } 36 37 38 public MasterWindowViewModel() 39 { 40 InputSttid= new ReactiveProperty<string>(); 41 InputEndid= new ReactiveProperty<string>(); 42 43 CheckBoxAll= new ReactiveProperty<bool>(); 44 45 SearchCommand = new ReactiveCommand(); 46 SearchCommand.Subscribe(SearchButtonOnClick); 47 48 MainDataGrid = new ReactiveProperty<DataTable>(); 49 50 ShowDataEditWindow = new ReactiveCommand(); 51 52 this.FormInit(); 53 } 54 55 /// <summary> 56 /// 検索ボタン押下イベント 57 /// </summary> 58 public void SearchButtonOnClick() 59 { 60 //データを取得 61 62 DataTable dt = new DataTable("mta"); 63 QueryObject qo = new QueryObject(); 64 qo.TableName = "mta"; 65 qo.Sort = "id"; 66 67 DataAccessManager.Fill(dt, qo); 68 69 MainDataGrid.Value = dt; 70 71 72 } 73 74 /// <summary> 75 /// 初期値の設定 76 /// </summary> 77 public void FormInit() 78 { 79 InputSttid.Value = "000"; 80 InputEndid.Value = "999"; 81 CheckBoxAll.Value = true; 82 83 } 84 85 } 86} 87 88
TN8001👍を押しています

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

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

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

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

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

guest

回答1

0

ベストアンサー

ここでDataGrid上で色々データを出力している中でDBテーブル上にコードしか持たないカラムをDataGridではコードを表示させず、固定の名称を出力したいと思っています。(コード:1の場合は「可」を表示、コード9は「不可」を表示)
どのような手段があるのか教えていただけませんでしょうか?

データベースでどうにかする・DataTableでどうにかする・表示時にどうにかする等いろいろありそうな気はします(わたしは趣味グラマなので何が一般的かはわかりません^^;

データベースも全然わからないので、表示時にどうにかする方法です。
DictionaryDataGridComboBoxColumnを使用します(DataGridComboBoxColumnは編集に難ありですが、ReadOnlyならいいでしょう)

xml

1<Window 2 x:Class="Qdgnxya2nxjfyfx.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 xmlns:vm="clr-namespace:Qdgnxya2nxjfyfx" 6 Title="{Binding Title.Value}" 7 Width="800" 8 Height="450" 9 Background="WhiteSmoke" 10 FocusManager.FocusedElement="{Binding ElementName=searchBottun}"> 11 <Window.DataContext> 12 <vm:MasterWindowViewModel /> 13 </Window.DataContext> 14 <Window.Resources> 15 <CollectionViewSource x:Key="TypeJp" Source="{Binding TypeJp}" /> 16 </Window.Resources> 17 <Grid> 18 <Grid.RowDefinitions> 19 <RowDefinition Height="Auto" /> 20 <RowDefinition /> 21 <RowDefinition Height="Auto" /> 22 </Grid.RowDefinitions> 23 <Border 24 Margin="20" 25 Padding="8" 26 HorizontalAlignment="Left" 27 BorderBrush="CornflowerBlue" 28 BorderThickness="1" 29 CornerRadius="10"> 30 <StackPanel Orientation="Horizontal"> 31 <GroupBox 32 Width="300" 33 Margin="0,-4,0,0" 34 Padding="4,0,0,0" 35 BorderBrush="CornflowerBlue" 36 Header="コード範囲"> 37 <StackPanel Orientation="Horizontal"> 38 <CheckBox 39 Margin="0,0,20,0" 40 VerticalAlignment="Center" 41 VerticalContentAlignment="Center" 42 Content="全件" 43 IsChecked="{Binding CheckBoxAll.Value}" /> 44 <TextBox 45 VerticalAlignment="Center" 46 MaxLength="3" 47 Text="{Binding InputSttid.Value}" /> 48 <Label Content="" /> 49 <TextBox 50 VerticalAlignment="Center" 51 MaxLength="3" 52 Text="{Binding InputEndid.Value}" /> 53 </StackPanel> 54 </GroupBox> 55 <Button 56 x:Name="searchBottun" 57 Grid.Column="1" 58 MinWidth="80" 59 Margin="8,16,8,8" 60 VerticalAlignment="Center" 61 Command="{Binding SearchCommand}" 62 Content="検索" /> 63 </StackPanel> 64 </Border> 65 <DataGrid 66 Grid.Row="1" 67 Margin="20,0,20,20" 68 AlternatingRowBackground="AliceBlue" 69 AutoGenerateColumns="False" 70 BorderBrush="CornflowerBlue" 71 IsReadOnly="True" 72 ItemsSource="{Binding MainDataGrid.Value}" 73 RowHeaderWidth="20" 74 SelectionMode="Single"> 75 <DataGrid.Columns> 76 <DataGridTextColumn 77 Width="100" 78 Binding="{Binding id}" 79 Header="コード" /> 80 <DataGridComboBoxColumn 81 Width="150" 82 DisplayMemberPath="Value" 83 Header="種別" 84 ItemsSource="{Binding Source={StaticResource TypeJp}}" 85 SelectedValueBinding="{Binding type}" 86 SelectedValuePath="Key" /> 87 <DataGridTextColumn 88 Width="150" 89 Binding="{Binding name}" 90 Header="名称" /> 91 <DataGridTextColumn 92 Width="150" 93 Binding="{Binding volume}" 94 Header="数量" /> 95 <DataGridTextColumn 96 Width="150" 97 Binding="{Binding display_order}" 98 Header="表示順" /> 99 </DataGrid.Columns> 100 </DataGrid> 101 <StatusBar Grid.Row="2"> 102 <Label Content="" /> 103 </StatusBar> 104 </Grid> 105</Window>

cs

1using System.Collections.Generic; 2using System.Data; 3using System.Windows; 4using Prism.Mvvm; 5using Reactive.Bindings; 6 7namespace Qdgnxya2nxjfyfx; 8 9 10public class MasterWindowViewModel : BindableBase 11{ 12 public ReactiveProperty<string> Title { get; } = new("マスタ登録"); 13 public ReactiveProperty<string> InputSttid { get; } = new("000"); 14 public ReactiveProperty<string> InputEndid { get; } = new("999"); 15 public ReactiveProperty<bool> CheckBoxAll { get; } = new(true); 16 public ReactiveProperty<DataTable> MainDataGrid { get; } = new(); 17 18 public ReactiveCommand SearchCommand { get; } = new(); 19 20 public Dictionary<int, string> TypeJp { get; } = new() 21 { 22 { 1, "可" }, 23 { 9, "不可" }, 24 }; 25 26 public MasterWindowViewModel() => SearchCommand.Subscribe(SearchButtonOnClick); 27 28 private void SearchButtonOnClick() 29 { 30 var dt = new DataTable(); 31 dt.Columns.Add("id"); 32 dt.Columns.Add("type"); 33 dt.Columns.Add("name"); 34 dt.Columns.Add("volume"); 35 dt.Columns.Add("display_order"); 36 37 dt.Rows.Add(1, 1, "名称1", "数量1", "表示順1"); 38 dt.Rows.Add(2, 9, "名称2", "数量2", "表示順2"); 39 40 MainDataGrid.Value = dt; 41 } 42} 43 44public partial class MainWindow : Window 45{ 46 public MainWindow() => InitializeComponent(); 47}

アプリ画像


また、MVVMやWPFやPrismやReactivePropertyを勉強するのにいいサイトやサンプルがあれば
教えてください!

わたしはWeb上の情報のみで十分学習できました(基本的には詰まったときにggるだけですw
mitu1122さんも既に見ているものも多いでしょうが...

WPF

ある程度まとまっているのはこのあたりでしょうか。
» WPF 学習用ドキュメント作りました

WPF 4.5入門 | 大田一希 | 工学 | Kindleストア | Amazon
WPF4.5入門のPDF - かずきのBlog@hatena

記事数は少ないですが、おしゃれで素敵。
uEN Laboratory

Prism

ぶっちゃけドキュメントもサンプルも物足りないですね^^;
Introduction to Prism | Prism
PrismLibrary/Prism-Samples-Wpf: Samples that demonstrate how to use various Prism features with WPF

ある程度の規模のアプリで、作者本人が書いているのはこちらです。
brianlagunas/PrismOutlook: The Prism for WpF app that we are building live on Twitch/YouTube
Prism for WPF - Building Outlook - YouTube

しかし有償のコントロールを使いまくってる上、本題でないところに無駄に凝っていてサンプルとしてはいまいちです^^;
有償のコントロールを雑に省いたフォーク TN8001/PrismOutlook: noig

日本語ではこちらが結構な量の記事を書かれてますね。
:: halation ghost ::

ReactiveProperty

見ているでしょうが、こちらで必要十分でしょう。
MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 前編 - Qiita
MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 中編 - Qiita
MVVM をリアクティブプログラミングで快適に ReactiveProperty オーバービュー 2020 年版 後編 - Qiita

不可解な挙動があったら「実はバグでした」みたいなこともあるので、そういう時はイシューも確認しましょう。
Issues · runceel/ReactiveProperty

投稿2022/12/06 22:00

編集2022/12/06 22:10
TN8001

総合スコア9326

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

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

mitu1122

2022/12/06 22:51

ありがとうございました! やりたかったことができそうです。 数日分からずモチベーションが下がってましたが、参考になるサイトなど知りたかった事が書いてあり頑張れそうです!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問