使用样式将文本框设置为只读 [英] Making textboxes readonly using style

查看:78
本文介绍了使用样式将文本框设置为只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#/WPF应用程序.在Mainwindow.xaml上,我正在以编程方式加载其他视图.
MainWindow.xaml上有一个checbox.单击它时,我需要将屏幕上的所有文本框控件都设置为只读.
该代码适用于Mainwindow上的控件,但不适用于AView.xaml上的文本框

请问我在这里想念的是什么?或者还有其他方法可以实现此功能吗?

谢谢

这是我的代码:

MainWindow.xaml:

    < CheckBox IsChecked =" {绑定IsCheckboxChecked,Mode =双向,UpdateSourceTrigger = PropertyChanged}"/>
                 
    < ContentPresenter Content =" {Binding CurrentViewModel}" >
                    < ContentPresenter.Resources>
                        < DataTemplate DataType =" {x:Type ViewModel:VModelA}"  >
                            < Views:AView/>
                        </DataTemplate>                    
                </ContentPresenter>

MainWindowResources.xaml:

    < Style TargetType =" TextBox" x:Key ="ReadOnlyStyle">
            < Setter Property ="IsReadOnly";值="{{Binding Path = IsCheckboxChecked}"/>        
        </Style>
    
    < Style TargetType =" TextBox" x:Key ="ReadOnlyStyleChild">
            < Setter Property ="IsReadOnly";值=" {Binding Path = IsCheckboxCheckedChild}"/>
        </Style>

MainWindowViewModel.cs

   私有静态MainWindowViewModel _instance = new MainWindowViewModel();
    
   公共静态MainWindowViewModel实例
            {
               得到{
                   返回_instance;
                }
            }
           
     公共布尔IsCheckboxChecked
            {
               得到{ 
                    
                   返回m_isCheckboxChecked;
                }
               设置
                {              
                    m_isCheckboxChecked =值;
                    OnPropertyChanged("IsCheckboxChecked");                
                }       
            }

VModelA.cs:

   公共布尔IsCheckboxCheckedChild
                {
                   得到{
                       返回MainWindowViewModel.Instance.IsCheckboxChecked;
                    }
                    
                }

AView.xaml

     < TextBox Style =" {DynamicResource ReadOnlyStyleChild}">

I've a C#/WPF application.On my Mainwindow.xaml,I'm programmatically loading other views.
There's a checbox on MainWindow.xaml.When its clicked, I need to make all textbox controls on the screen as readonly.
The code is working for controls on Mainwindow but not for textboxes on AView.xaml

What am I missing here please?Or is there any other way of achieving this functionality?

Thanks.

Here' my code:

MainWindow.xaml:

    <CheckBox IsChecked="{Binding IsCheckboxChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                 
    <ContentPresenter Content="{Binding CurrentViewModel}" >
                    <ContentPresenter.Resources>
                        <DataTemplate DataType="{x:Type ViewModel:VModelA}"  >
                            <Views:AView/>
                        </DataTemplate>                    
                </ContentPresenter>

MainWindowResources.xaml:

    <Style TargetType="TextBox" x:Key="ReadOnlyStyle">
            <Setter Property="IsReadOnly" Value="{Binding Path=IsCheckboxChecked}"/>        
        </Style>
    
    <Style TargetType="TextBox" x:Key="ReadOnlyStyleChild">
            <Setter Property="IsReadOnly" Value="{Binding Path=IsCheckboxCheckedChild}"/>
        </Style>

MainWindowViewModel.cs

    private static MainWindowViewModel _instance = new MainWindowViewModel();
    
    public static MainWindowViewModel Instance
            {
                get {
                    return _instance;
                }
            }
           
     public bool IsCheckboxChecked
            {
                get { 
                    
                    return m_isCheckboxChecked;
                }
                set
                {               
                    m_isCheckboxChecked = value;
                    OnPropertyChanged("IsCheckboxChecked");                
                }        
            }

VModelA.cs:

    public bool IsCheckboxCheckedChild
                {
                    get {
                        return MainWindowViewModel.Instance.IsCheckboxChecked;
                    }
                    
                }

AView.xaml

     <TextBox Style="{DynamicResource ReadOnlyStyleChild}">

推荐答案

请标记为有帮助来关闭线程帖子作为答案?

Could you please close your threads by marking helpful posts as answer?

>>该代码适用于Mainwindow上的控件,但不适用于AView.xaml上的文本框

那是因为您没有为IsCheckboxCheckedChild属性引发任何PropertyChanged事件.您的VModelA类具有对MainWindowViewModel的依赖关系.如果您希望每当IsCheckboxChecked属性刷新AView中的绑定 更改父视图模型的值时,无论何时发生这种情况,您都需要为VModelA中的IsCheckboxCheckedChild属性提高PropertyChanged.您可以通过在构造函数中为父视图模型的PropertyChanged事件挂接事件处理程序来完成此操作 VModelA子视图模型的示例:

That's because you are not raising any PropertyChanged event for the IsCheckboxCheckedChild property. Your VModelA class has a dependency upon MainWindowViewModel. If you want the binding in AView to get refreshed whenever the IsCheckboxChecked property of the parent view model is changed, you need to raise the PropertyChanged for the IsCheckboxCheckedChild property in VModelA whenever this happens. You can do this by hooking up an event handler for the parent view model's PropertyChanged event in the constructor of the VModelA child view model:

public VModelA()
        {
            MainWindowViewModel.Instance.PropertyChanged += (s, e) => 
            {
                if(e.PropertyName == "IsCheckboxChecked")
                {
                    OnPropertyChanged("IsCheckboxCheckedChild");
                }
            };
        }


请注意,VModelA必须实现INotifyPropertyChanged事件,该事件才能正常运行,并且要刷新IsReadOnly属性绑定.

Note that VModelA must implement the INotifyPropertyChanged event for this to work and for the IsReadOnly property binding to get refreshed.


希望有帮助.


Hope that helps.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于使用样式将文本框设置为只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆