c# - 如何从 Caliburn.Micro ViewModel 制作文本框 ReadOnly?

标签 c# windows-phone-7 mvvm caliburn.micro

我开始使用 Caliburn.Micro 在 WP 7.1 上编写应用程序。我的起始 View 是一个带有登录和传递文本框的日志窗口以及带有登录操作的按钮。现在我想将顶部的进度条显示为不确定并将文本框设置为只读。我一直在网上搜索,发现只有不满意的解决方案。我正在考虑这样的解决方案:

  • 更改默认绑定(bind)器以按约定绑定(bind)属性,例如“ControlNameIsReadOnly”“ControlNameIsEnabled”
  • 将屏幕设为对话框,仅显示进度条

  • 有人解决了这个问题吗?这些解决方案都没有让我满意。

    最佳答案

    有几种方法-取决于您是否要基于约定和按控件进行操作

    如果没有约定,您可以使用繁忙的属性,例如

    class SomeViewModel : Screen // or whatever
    {
        public bool IsBusy 
        {
            get { // getter code 
            } 
            set { // setter code, calling notifypropertychanged 
            }
    
    }
    

    然后在你的控制
    <UserControl>
        <Grid>
            <TextBox>Hello World</TextBox>
            <Border Background="#AAFFFFFF"> <!-- Semitransparent overlay -->
                <ProgressBar IsIndeterminate="true" Visibility="{Binding IsBusy, Converter={StaticResource BooleanToVisibilityConverter}" />
            </Border>
        </Grid>
    </UserControl>
    

    您的转换器可能如下所示:
    class BooleanToVisibilityConverter : IValueConverter
    {
        public void Convert(object value, blah blah blah...)
        {
            if(value is bool)
            {
                if((bool)value) return Visibility.Visible;
    
                return Visibility.Collapsed;
            }
    
            return null;
        }
    }
    

    这样,您可以在控件上发生事情时覆盖进度条,以便用户无法交互。

    您可能需要调整以使其最适合,因为我没有测试过任何一个:P

    你甚至可以绑定(bind)IsReadOnlyIsBusy 的文本框中属性(property)。这当然是假设您不介意使用非常规方法并且它在 Windows Phone 7 上都可以工作:)

    关于c# - 如何从 Caliburn.Micro ViewModel 制作文本框 ReadOnly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13620589/

    相关文章:

    c# - 将 JSON 字符串转换为 JSON 对象 c#

    windows-phone-7 - IsolatedStorageFile.GetUserStoreForApplication().AvailableFreeSpace 错了吗?

    WPF BindingExpression System.Windows.Data 错误 : Need filename or class name (Dependency Injection/MEF)

    c# - 如何将 X 按钮绑定(bind)到窗口的关闭按钮

    c# - 我们可以在 WPF MVVM 中使用 <i :Interaction. Triggers>(不在 Silverlight 中)

    c# - 在 C# 中将 Stream 转换为 FileStream

    c# - List.Insert 有任何性能损失吗?

    c# - 如何在 Windows Phone 7 中添加 Flurry Agent?

    c# - 如何将 nest 1.x 中返回的搜索结果映射到一个对象?

    c# - 按下后退按钮时 Windows Phone 取消异步任务