0
votes

I have a situation: DataGrid with TemplateColumn. Template contains Stack panel with CheckBox and TextBox. What I want is when I press CheckBox then TextBox which is on the same StakcPanel set value to 1. So in simple CheckBox -> IsChecked==true -> Set TextBox value to 1 (TextBox on the same StackPanel as CheckBox)

I try with RelativeSource but not work. Thanks For help.

1

1 Answers

1
votes

Well you can bind the Content of the TextBox TextForTextBox, and bind the Checked state to Checked. in your viewModel you would : do

private string _text;
    private bool _checked;

    public string TextForTextBox
    {
        get { return _text; }
        private set { _text = value;}
    }

    public bool Checked
    {
        get { return _checked; }
        set
        {
            if (_checked != value)
            {
                _checked = value;
                Text = _checked ? 1 : = 0;
                PropertyChanged("Checked");
                PropertyChanged("TextForTextBox");
            }
        }
    }

This is assuming that you have a ViewModel that implements the INotfiyPropertyChanged.

EDIT Code to change this in pure XAML :

 <Grid>
    <StackPanel>
        <CheckBox x:Name="check" />
        <TextBox>
            <TextBox.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=check, Path=IsChecked}" Value="True">
                            <Setter Property="TextBox.Text" Value="1" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=check, Path=IsChecked}" Value="False">
                            <Setter Property="TextBox.Text" Value="0" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
    </StackPanel>
</Grid>