【发布时间】:2019-08-11 00:23:48
【问题描述】:
现在我有一个具有多个不同条目的ComboBox,并且选择时,我想要一个DataGrid我必须根据来自ComboBox的所选文本更改列宽度。到目前为止,我已经尝试在我的窗口资源下的样式中使用转换器,但是,我的列的宽度不会根据输入的文本而改变,而是设置回自动。这是我的转换器:
public class BindingWidthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var Notation = value as string;
if (Notation == null) return 26;
switch (Notation)
{
case "size 1":
return 26;
case "size 2":
return 40;
case "size 3":
return 45;
case "size 4":
return 50;
case "size 5":
return 60;
default:
return 26;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是它在我的 XAML window.resource 下的定义方式:
<Style x:Key="ElementStyle" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="{Binding NotationType, UpdateSourceTrigger=PropertyChanged, IsAsync=True, Mode=TwoWay, Converter={StaticResource WidthConv}, ConverterParameter=0}"/>
</Style>
然后将其输入到我的 DataGrid.Column 部分:
<DataGridTextColumn Header="0" Binding="{Binding DataSpace, UpdateSourceTrigger=PropertyChanged, IsAsync=True, Converter={StaticResource DataConv}, ConverterParameter=0}"
ElementStyle="{StaticResource ElementStyle}"
CellStyle="{StaticResource CellStyle0}"
HeaderStyle="{StaticResource HeaderStyle}"/>
有人可以帮忙吗?
【问题讨论】:
标签: c# wpf mvvm datagrid converters