WPF の View-Model-ViewModel パターンの下で、グリッド コントロールのさまざまな定義の Heights と Widths をデータバインドしようとしているので、GridSplitter を使用した後にユーザーが設定した値を保存できます。ただし、通常のパターンは、これらの特定のプロパティでは機能しないようです。
注:これは、Googleが失敗したため、私が投稿している参照の質問として投稿しています。これは自分で解決する必要がありました. 従うべき私自身の答え。
WPF の View-Model-ViewModel パターンの下で、グリッド コントロールのさまざまな定義の Heights と Widths をデータバインドしようとしているので、GridSplitter を使用した後にユーザーが設定した値を保存できます。ただし、通常のパターンは、これらの特定のプロパティでは機能しないようです。
注:これは、Googleが失敗したため、私が投稿している参照の質問として投稿しています。これは自分で解決する必要がありました. 従うべき私自身の答え。
次のように を作成IValueConverter
します。
public class GridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double val = (double)value;
GridLength gridLength = new GridLength(val);
return gridLength;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
GridLength val = (GridLength)value;
return val.Value;
}
}
その後、バインディングでコンバーターを利用できます。
<UserControl.Resources>
<local:GridLengthConverter x:Key="gridLengthConverter" />
</UserControl.Resources>
...
<ColumnDefinition Width="{Binding Path=LeftPanelWidth,
Mode=TwoWay,
Converter={StaticResource gridLengthConverter}}" />
私が発見した多くの落とし穴がありました:
したがって、次のコードを使用しました。
private GridLength myHorizontalInputRegionSize = new GridLength(0, GridUnitType.Auto)
public GridLength HorizontalInputRegionSize
{
get
{
// If not yet set, get the starting value from the DataModel
if (myHorizontalInputRegionSize.IsAuto)
myHorizontalInputRegionSize = new GridLength(ConnectionTabDefaultUIOptions.HorizontalInputRegionSize, GridUnitType.Pixel);
return myHorizontalInputRegionSize;
}
set
{
myHorizontalInputRegionSize = value;
if (ConnectionTabDefaultUIOptions.HorizontalInputRegionSize != myHorizontalInputRegionSize.Value)
{
// Set the value in the DataModel
ConnectionTabDefaultUIOptions.HorizontalInputRegionSize = value.Value;
}
OnPropertyChanged("HorizontalInputRegionSize");
}
}
そしてXAML:
<Grid.RowDefinitions>
<RowDefinition Height="*" MinHeight="100" />
<RowDefinition Height="Auto" />
<RowDefinition Height="{Binding Path=HorizontalInputRegionSize,Mode=TwoWay}" MinHeight="50" />
</Grid.RowDefinitions>
最も簡単な解決策は、これらのプロパティに文字列設定を使用するだけで、WPF が GridLengthConverter を使用して追加の作業なしで自動的にサポートするようにすることです。
GridLength
との間の変換を行ったので、別の可能性は、int
を作成し、IValueConverter
にバインドするときにそれを使用することWidth
です。■とメソッドIValueConverter
の両方があるため、双方向バインディングも処理します。ConvertTo()
ConvertBack()