Q&A
現在、.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
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2022/12/06 22:51