gpt4 book ai didi

c# - 导致 xamDataGrid 重新绘制

转载 作者:太空狗 更新时间:2023-10-30 01:24:49 31 4
gpt4 key购买 nike

我需要一种方法让 View 模型指示 View 上的 XamDataGrid 以最少的麻烦重新读取和重新绘制其单元格。我不想弄乱源代码并做一些不可持续的解决方法来引发它的事件(源代码可能会改变)。

为了让它更容易理解,我有一个全局静态类,它包含一些视觉提示配置,这些配置不会影响数据,但只会影响它们在网格中的表示方式(缩放、格式化等)。视觉操作发生在附加到工作正常的字段的 IValueConverter 实现中。有一个静态事件,当提示发生变化并且 View 模型订阅它并且事件正确触发时会触发。现在我只需要让事件处理程序使网格重新绘制。

有什么建议吗?

编辑:一些代码,如果有帮助的话:

转换器:

public class ScaleConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType == typeof(double) || targetType == typeof(double?))
{
if (value == null && targetType == typeof(double?)) return null; // short circuit for null->null, nothing to scale
if (value is double) return (double)value / DisplayScale.Scale; // shortcut for direct scaling, no need to waste time on conversion
try
{
return System.Convert.ToDouble(value) / DisplayScale.Scale; // for convertible values, eat conversion exception
}
catch (Exception) {};
// if all esle fails return NaN
return double.NaN;
}
// fallthrough, return null, this should not happen, if it does it means converter is incomplete
return null;
}
...
}

全局显示比例

public class DisplayScale: NotificationObject
{
private static KeyValuePair<string, double> _ActiveScaling;
private static readonly object _lockHandle = new object(); // note: should not contest, but just to be on the safe side
public static event Action ScalingChanged;

public static List<KeyValuePair<string, double>> ScaleList { get; private set; }

static DisplayScale()
{
ScaleList = new List<KeyValuePair<string, double>>()
{
new KeyValuePair<string, double>("No scaling (1's)", 1d),
new KeyValuePair<string, double>("Thousands (1,000's)", 1000d),
new KeyValuePair<string, double>("Millions (1,000,000's)", 1000000d),
new KeyValuePair<string, double>("Billions (1,000,000,000's)", 1000000000d)
};
ActiveScaling = ScaleList.First(); // OPTION: could be in per-user config
}

public static double Scale { get { return ActiveScaling.Value; } }
public static KeyValuePair<string, double> ActiveScaling
{
get
{
lock (_lockHandle) return _ActiveScaling;
}
set
{
lock (_lockHandle)
{
_ActiveScaling = value;
var eventCall = ScalingChanged;
if (eventCall != null) eventCall();
}
}
}
}

字段定义为

// resource
<inf:ScaleConverter x:Key="ScaleConverter" />
// field
<igDP:Field Name="Deposit" Converter="{StaticResource ScaleConverter}">

最佳答案

如果你有一个 collectionview 而不是简单地调用它的 Refresh()。

public class YourViewModel
{
private ObservableCollection<YourDataClass> yourColl;

public void YourViewModel()
{
yourColl = new ObservableCollection<YourDataClass>();
YourCollectionView = CollectionViewSource.GetDefaultView(yourColl);
DisplayScale.ScalingChanged += () => YourCollectionView.Refresh();
}

var ICollectionView yourCollView;
public ICollectionView YourCollectionView
{
get { yourCollView; }
set {
yourCollView = value;
RaisePropertyChanged("YourCollectionView");
}
}
}

关于c# - 导致 xamDataGrid 重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8374694/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com