tags:

views:

80

answers:

2

I have a scenario where based on the textbox's text value I have to disable and enable the button say, for TextBox.Text="abc" or "cdf" the button should be disabled and for other values it should be enabled.

this has to be written only in Xaml.

Thanks In Advance

A: 

This is not possible to do strictly in XAML, and nor does such a requirement make sense. This is business logic that should be manifested in a view model:

public class MyViewModel : ViewModel
{
    private string _text;

    public string Text
    {
        get { return _text; }
        set
        {
            if (_text != value)
            {
                _text = value;
                OnPropertyChanged("Text");
                OnPropertyChanged("IsButtonEnabled");
            }
        }
    }

    public bool IsButtonEnabled
    {
        get { return _text != "abc"; }
    }
}

Then, in your XAML:

<TextBox Text="{Binding Text}"/>
<Button IsEnabled="{Binding IsButtonEnabled}"/>

HTH, Kent

Kent Boogaart
+1  A: 

Looks like you can use Triggers to do this:

Button gets disabled when the value ABC is entered in the textbox and then get enabled when the value changes to something other then ABC.

<Window x:Class="WpfApplication5.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">

<Window.Resources>
    <Style x:Key="disableButton" TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ElementName=textBox1,Path=Text}" Value="ABC">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>

        </Style.Triggers>
    </Style>
</Window.Resources>

<StackPanel>
    <TextBox x:Name="textBox1"/>
    <Button Style="{StaticResource disableButton}" Height="23" Name="button1" Width="75">Button</Button>
</StackPanel>

Chris Persichetti