ViewModel 如何在需要时从视图请求数据?

发布于 2024-11-29 06:46:26 字数 131 浏览 1 评论 0原文

我的视图上有一个计算属性,我需要将其绑定到我的 ViewModel。我正在使用 WPF,似乎无法创建可自计算的可绑定属性(依赖属性)。我不想在视图状态发生变化时执行计算,因为它们非常耗时。我想在 ViewModel 需要结果时(即关闭时)进行计算。

I have a calculated property on my View that I need to bind to my ViewModel. I'm using WPF and it seems that there is no way to make a bindable property (Dependency Property) that is self calculating. I don't want to perform the calculations whenever the View's state changes because they are time intensive. I want to do the calculations whenever the ViewModel needs the result, i.e. when it closes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话已封尘 2024-12-06 06:46:26

根据您上面的评论,我将使用 Converter

您的 ViewModel 将包含加密数据,并且与视图的绑定使用 Converter 将其转换为可读的内容。当需要将数据保存回 ViewModel 时,请使用转换器的 ConvertBack 方法再次加密数据。

<TextBox Text="{Binding EncryptedAccountNumber, 
         Converter={StaticResource DecryptTextConverter}}" />
public class DecryptTextConverter: IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Implement decryption code here
        return decryptedValue;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Implement encryption code here
        return ecryptedValue;
    }
}

如果加密代码需要一段时间,请设置 UpdateSourceTrigger=Explicit 并在单击“保存”按钮时手动触发源更新。

Based on your comment above, I'd use a Converter

Your ViewModel would contain the encrypted data, and the binding to the View uses a Converter which converts it into something readable. When it's time to save the data back to the ViewModel, use the ConvertBack method of the converter to encrypt the data again.

<TextBox Text="{Binding EncryptedAccountNumber, 
         Converter={StaticResource DecryptTextConverter}}" />
public class DecryptTextConverter: IValueConverter
{
    public object Convert(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Implement decryption code here
        return decryptedValue;
    }

    public object ConvertBack(object value, Type targetType, 
        object parameter, CultureInfo culture)
    {
        // Implement encryption code here
        return ecryptedValue;
    }
}

If the Encryption code takes a while, set your UpdateSourceTrigger=Explicit and manually trigger the source update when the Save button is clicked.

梦醒时光 2024-12-06 06:46:26

这是我的解决方案。它的工作方式与 ICommand 相同,但视图提供委托 (CalculationDelegate),并且视图模型调用 CanExecuteExecute.它不是纯粹的 MVVM,但它可以工作。

public interface ICalculationProvider<TResult>
{
    event EventHandler CanExecuteChanged;

    Func<TResult> CalculationDelegate { get; set; }

    bool CanExecute();
    TResult Execute();
    bool TryExecute(out TResult a_result);
} 

我已将 Rachel 的答案标记为正确,只是因为我在这里所做的不是纯粹的 MVVM。

This is my solution. It works the same way as ICommand but the view provides the delegate (CalculationDelegate) and the view model calls CanExecute and Execute. Its not pure MVVM but it works.

public interface ICalculationProvider<TResult>
{
    event EventHandler CanExecuteChanged;

    Func<TResult> CalculationDelegate { get; set; }

    bool CanExecute();
    TResult Execute();
    bool TryExecute(out TResult a_result);
} 

I have marked Rachel's answer as correct, simply because what I am doing here is not pure MVVM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文