现在我有一个带有多个不同条目的组合框,当我选择了想要数据网格时,我必须根据组合框中的选定文本更改列宽。到目前为止,我已经尝试在我的窗口资源下使用样式中的转换器,但是,我的列的宽度不会根据输入的文本而改变,而是设置回Auto。这是我的转换器:
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}"/>
有人能帮忙吗?
您是否尝试在DataGrid宽度本身而不是TextBlock中使用宽度转换器?
<DataGridTextColumn Header="0"
Binding="{Binding DataSpace, UpdateSourceTrigger=PropertyChanged, IsAsync=True, Converter={StaticResource DataConv}, ConverterParameter=0}"
ElementStyle="{StaticResource ElementStyle}"
Width="{Binding NotationType, UpdateSourceTrigger=PropertyChanged, IsAsync=True, Mode=TwoWay, Converter={StaticResource WidthConv}}"
CellStyle="{StaticResource CellStyle0}"
HeaderStyle="{StaticResource HeaderStyle}"/>
我认为NotationType
是与DataSpace
相同的物品。
让我知道这个是否奏效。