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

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

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

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

Q&A

解決済

2回答

3165閲覧

DataGridTextColumn Width Binding のやり方

wadamaruz01

総合スコア22

C#

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

1グッド

0クリップ

投稿2021/05/13 12:46

DataGridTextColumnのWidthのバインドができずに困っています。
aaaの部分にはVMクラスのプロパティに紐づけたいのですがやり方がわからず困っています。

<DataGridTextColumn Width="{Binding aaa}" >
TN8001👍を押しています

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

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

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

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

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

guest

回答2

0

ベストアンサー

aaaの部分にはVMクラスのプロパティに紐づけたいのですがやり方がわからず困っています。

質問文は単純ですが2つの要素が合わさっています。

1つ目はビジュアルツリーの問題です。

DataGridColumnはビジュアルツリーに含まれないため、素直にはViewModelにアクセスできません。
ツリー - WPF .NET Framework | Microsoft Docs

よくある手法は中継役(BindingProxy)を用意して、それ経由でアクセスします。

x:Referenceでコントロールを参照しても動きました。
c# - Binding datagrid column width - Stack Overflow


2つ目はDataGridLengthの問題です。

aaaの型がわかりませんが、おそらくdouble等数値を想定されていると思います。
DataGridLengthStarAuto指定できるので、単なる数値ではありません
DataGridLength 構造体 (System.Windows.Controls) | Microsoft Docs

TwoWayでバインドするには双方向のコンバータが必要です。
wpf - How do I databind a ColumnDefinition's Width or RowDefinition's Height? - Stack Overflow

xml

1<Window 2 x:Class="Questions338171.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:Questions338171" 6 Width="800" 7 Height="450"> 8 <Window.DataContext> 9 <local:ViewModel /> 10 </Window.DataContext> 11 <Window.Resources> 12 <local:BindingProxy x:Key="proxy" Data="{Binding}" /> 13 <local:DataGridLengthConverter x:Key="dataGridLengthConverter" /> 14 </Window.Resources> 15 <DockPanel> 16 <Slider 17 x:Name="slider" 18 DockPanel.Dock="Top" 19 Maximum="{Binding ActualWidth, RelativeSource={RelativeSource self}}" 20 Value="{Binding Width}" /> 21 <TextBlock DockPanel.Dock="Top" Text="{Binding Width}" /> 22 <DataGrid AutoGenerateColumns="False"> 23 <DataGrid.Columns> 24 <DataGridTextColumn 25 Width="{Binding Data.Width, Converter={StaticResource dataGridLengthConverter}, Mode=TwoWay, Source={StaticResource proxy}}" 26 Binding="{Binding}" 27 Header="BindingProxy" /> 28 <DataGridTextColumn 29 Width="{Binding Value, Converter={StaticResource dataGridLengthConverter}, Mode=TwoWay, Source={x:Reference slider}}" 30 Binding="{Binding}" 31 Header="x:Reference" /> 32 </DataGrid.Columns> 33 aaaa 34 </DataGrid> 35 </DockPanel> 36</Window>

cs

1using CommunityToolkit.Mvvm.ComponentModel; 2using System; 3using System.Globalization; 4using System.Windows; 5using System.Windows.Controls; 6using System.Windows.Data; 7 8namespace Questions338171 9{ 10 class BindingProxy : Freezable 11 { 12 public object Data { get => GetValue(DataProperty); set => SetValue(DataProperty, value); } 13 public static readonly DependencyProperty DataProperty 14 = DependencyProperty.Register(nameof(Data), typeof(object), typeof(BindingProxy), 15 new PropertyMetadata(null)); 16 protected override Freezable CreateInstanceCore() => new BindingProxy(); 17 } 18 19 class DataGridLengthConverter : IValueConverter 20 { 21 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 22 => new DataGridLength((double)value); 23 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 24 => ((DataGridLength)value).Value; 25 } 26 27 class ViewModel : ObservableObject 28 { 29 private double width = 150; 30 public double Width { get => width; set => SetProperty(ref width, value); } 31 } 32 33 public partial class MainWindow : Window 34 { 35 public MainWindow() => InitializeComponent(); 36 } 37}

こちらを使用しています。
NuGet Gallery | CommunityToolkit.Mvvm 7.0.2

投稿2021/05/13 21:51

編集2023/07/27 13:46
TN8001

総合スコア9396

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

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

0

丁寧にありがとうございました。できました!

投稿2021/05/18 12:25

wadamaruz01

総合スコア22

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問