0

I need to bind a group of textboxes with some data selected from combobox , but I don't want to reflect any changes on the TextBoxes to the ItemsSource of the combobox, so I set the Binding Mode to OneTime, it works fine, but I have a button that clears the contents of the textboxes, when clicked it keeps them clear even if I select an item from the combobox:

XAML:

  <Grid Name="mGrd" DataContext="{Binding ElementName=cmbBooks, Path=SelectedItem}" Grid.Column="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <ComboBox Name="cmbBooks" Grid.ColumnSpan="2" DisplayMemberPath="Title"/>

            <Label Grid.Row="1">Id</Label>
            <TextBox Grid.Row="1" Grid.Column="1" Name="txtId" Text="{Binding Path=Id, Mode=OneTime}"/>

            <Label Grid.Row="2">Title</Label>
            <TextBox Grid.Column="1" Grid.Row="2" Name="txtTitle" Text="{Binding Path=Title, Mode=OneTime}"/>

            <Label Grid.Row="3">#Pages</Label>
            <TextBox Grid.Column="1" Grid.Row="3" Name="txtPCount" Text="{Binding Path=PagesCount, Mode=OneTime}"/>

            <Label Grid.Row="4">Is Published</Label>
            <CheckBox Grid.Column="1" Grid.Row="4" VerticalAlignment="Center" Name="chkPblshd" IsChecked="{Binding Path=IsPublished, Mode=OneTime}"/>

            <StackPanel Grid.Row="5" Orientation="Horizontal">
                <Button Name="btnClear" Click="btnClear_Click" Background="Red" Foreground="White" FontWeight="Bold" Margin="2">X</Button>
                <Button Name="btnAddBook" Click="btnAddBook_Click">Add new Book</Button>
            </StackPanel>
        </Grid>

Clear button:

private void btnClear_Click(object sender, RoutedEventArgs e)
{
    txtId.Text = txtPCount.Text = txtTitle.Text = "";
    chkPblshd.IsChecked = false;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
mshwf
  • 7,009
  • 12
  • 59
  • 133
  • Assuming your original binding is to some source view model, I concur with the posted answer below. You should be setting the binding to `OneWay` and clearing the source property, not the target property. This will clear the target property as well, without destroying the binding. – Peter Duniho Jun 28 '17 at 22:19
  • If for some reason you _must_ destroy the binding and need to recreate it later, there is no shortage of questions with answers on Stack Overflow addressing that exact scenario. For example, https://stackoverflow.com/questions/2938656/binding-properties-in-code-behind, https://stackoverflow.com/questions/10131637/binding-string-property-in-code-behind-textblock, and https://stackoverflow.com/questions/4966967/wpf-how-to-set-checkbox-ischecked-binding-in-code-behind – Peter Duniho Jun 28 '17 at 22:19
  • I set the `SelectedItem` of the combobox to `null`, and it works! Is that the right way of clearing the target value? – mshwf Jun 28 '17 at 22:31
  • I hate the idea of inconsistency between Source and UI. – Peter Jun 28 '17 at 23:06
  • 1
    Answers such as those found on the marked duplicates address your direct question. However, do note that this is not the ideal way to deal with this. Your bindings should be set up once, and then manipulation of a view model property should be used to change the state of the view. – Peter Duniho Jun 29 '17 at 00:20

1 Answers1

0

Use binding mode OneWay instead of OneTime so changes in the model will reflect in the UI.

Unfortunately that's not enough in your case as you are setting the Text property manually. This is destroying your bindings! Clear the text in the bound values instead.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • changed and still no change – mshwf Jun 28 '17 at 22:09
  • @MohamedAhmed Oh, I didn't see your clear button. You can't change the text like that! See my edit – BradleyDotNET Jun 28 '17 at 22:13
  • Isn't the `Text` property the bound value and that's how I clear its value?! – mshwf Jun 28 '17 at 22:21
  • @MohamedAhmed No! The Text property is the just that; the Text property of the text box. The XAML sets that to a `Binding` object so the only correct way to modify after that is to modify the source of the binding. Changing the property itself destroys the link between the `Binding` object and the `TextBox` – BradleyDotNET Jun 28 '17 at 22:23