English 中文(简体)
文本可在纽伦。 Length> 仅在案文行文时,方言中方言 方框失去重点——为什么?
原标题:Enable button when text.Length > 0 in WPF only works when textBox loses focus - why?
  • 时间:2010-06-25 06:00:04
  •  标签:
  • binding

Simple WPF question I have a text Box and an OK Button and I am using a MVVM pattern. All I want to do is enable OK button only when textBox.Length > 0.

但是,我所看到的只是案文。 箱子失去着眼点。

我失踪了吗?

I have bound the datacontext to the viewmodel and done the following. Why does it not work?

感谢您的帮助

XAML

<TextBox Grid.Column="1" 
              Grid.Row="2" 
             Margin="4" 
             Name="txtName" Text="{Binding Path=Name}"/>

      <Button x:Name="btnOK" 
                            MinWidth="70" Padding="3.5" 
                            Margin="3.5" 
                            Grid.Column="1" 
                            Content="OK" 
                            Click="OnOk"
                            Command="{Binding Path=OKCommand}"
                            VerticalAlignment="Center" 
                            HorizontalAlignment="Left" IsDefault="True" />

In view Model

         public class TestViewModel : ViewModelBase
            {
                private string _name;

                public string Name
                {
                    get { return _name; }
                    set
                    {
                        _name = value;
                        OnPropertyChanged("Name");
                    }
                }
                private RelayCommand _OkCommand;
                public ICommand OKCommand
                {
                    get
                    {
                        return _OkCommand ?? (_OkCommand = new RelayCommand(x => Execute(), x => CanExecute));
                    }
                }
                private bool CanExecute
                {
                    get
                    {
                        return !string.IsNullOrEmpty(Name);            
                    }
                }
                private void Execute()
                {
                    //do something here
                }
最佳回答

交换这条线:

Text="{Binding Path=Name}"

页: 1

Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}"

这样,这种约束力将随时更新案文的修改。 案文Box的原始财产被丢失。

问题回答

暂无回答




相关问题
Get DynamicResource Binding in WPF

Can any one help me to get DynamicResource Binding in WPF by code? I have set binding Like follow, TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml. and i need to get the - TB s ...

WPF Binding to specific items in collection

I m currently trying to bind to certain items in a collection in wpf. This is best explained by an example. My XAML is below: <Canvas Name="TaskCanvas" Width="467.667" Height="414"> <...

WPF Dynamic Binding X and Y Co-ordinates

I have a question on WPF dynamic positioning. I want to place Elipses on the screen based on X and Y co-ordinates that i have stored in a collection in C#. I have been made aware of the drawing ...

WPF Data Binding Error in ListBox

I have a ListBox: <ListBox x:Name="HistogramListBox" Grid.Column="1" Margin="8,2,8,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Template="{StaticResource ...

WCF binding error

So I got into work early today and got the latest from source control. When I try to launch our ASP.NET application, I get this exception: "The binding at system.serviceModel/bindings/wsHttpBinding ...

silverlight 3 collection binding

Someone please help me understand why this binding does not work... I have a class called SelectionManager with a property called dates which is populated by a WCF service. The property is an ...

热门标签