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;
}