Home > Net >  Bind double property to the width of datagridtextcloumn, the width only increases but does not decre
Bind double property to the width of datagridtextcloumn, the width only increases but does not decre

Time:04-08

Bind double to the width of datagridtextcloumn, the width only increases but does not decrease. Why is this happening? Can anyone help me change the code?

MainWindow.xaml:

<Window.Resources>
        <local:ViewModel x:Key="vm"/>
        <local:MyMultiValueConverter x:Key="MyMultiValueConverter"/>
    </Window.Resources>
    <StackPanel DataContext="{StaticResource vm}">
        <DataGrid Name="dg" ItemsSource="{Binding View}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Col1"  Binding="{Binding SomeString}"  MinWidth="30" MaxWidth="300"  >
                    <DataGridTextColumn.Width>
                        <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                            <Binding Path="WidthValue" Source="{StaticResource vm}"/>
                            <Binding Path="WidthDefault" Source="{StaticResource vm}"/>
                        </MultiBinding>
                    </DataGridTextColumn.Width>
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="TextAlignment" Value="Center"/>
                            <Setter Property="Width">
                                <Setter.Value>
                                    <MultiBinding Converter="{StaticResource MyMultiValueConverter}">
                                        <Binding Path="WidthValue" Source="{StaticResource vm}"/>
                                        <Binding Path="WidthDefault" Source="{StaticResource vm}"/>
                                    </MultiBinding>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
        <CheckBox IsChecked="{Binding WidthDefault}" Content="Default Width"/>
        <Slider Width="200" Height="20" Minimum="5"  Maximum="300" Value="{Binding WidthValue}"/>
        <!--<TextBox Text="{Binding WidthValue}"/>-->
    </StackPanel>

Codebehind:

public class ViewModel : INotifyPropertyChanged
  {
    public ViewModel()
    {
      ObservableCollection<Data> col = new ObservableCollection<Data>();
       col.Add(new Data() { SomeString = 44 });
       col.Add(new Data() { SomeString = 84 });
       col.Add(new Data() { SomeString = 104 });
      cvs.Source = col;
    }
    private CollectionViewSource cvs = new CollectionViewSource();
    public ICollectionView View { get => cvs.View; }

    private double _widthValue = 30;
    public double WidthValue
    {
      get => this._widthValue;
      set { this._widthValue = value; OnPorpertyChanged(); }
    }

    private bool _widthDefault = false;
    public bool WidthDefault
    {
      get => this._widthDefault;
      set { this._widthDefault = value; OnPorpertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    internal void OnPorpertyChanged([CallerMemberName] string propName = "") => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));

  }

  public class Data
  {
    public double SomeString { get; set; }
  }

  public class MyMultiValueConverter : IMultiValueConverter
  {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
      double w =30;
      if (!(bool)values[1]) double.TryParse(values[0].ToString(), out w);
      return w;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
  }

When sliding the Slider to increase the WidthValue, the Width of the DataGridTextColumn increases:

enter image description here

When sliding the Slider to reduce the WidthValue, the Width of the DataGridTextColumn is not reduced:

enter image description here

CodePudding user response:

You are facing the issue because of binding for DataGridTextColumn.Width doesn't work.

The cause for it is that DataGridTextColumn.Width is of type DataGridLength, TextBlock.Width in it's turn is of type double and the binding does work here.

So as you increase the width of TextBlock, the width of column been increased automatically, but the column's width does not shrink on change of TextBlock.Width.

What you can do is to change return value of multyvalueconverter:

public class MyMultiValueConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   {
     double w =30;
     if (!(bool)values[1]) double.TryParse(values[0].ToString(), out w);
     return new DataGridLength(w);
   }
   public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   {
     throw new NotImplementedException();
   }
}

and then you don't need the binding for the TextBlock.Width, you can just remove it as unnecessary.

  • Related