我需要绑定一组文本框,其中包含从组合框中选择的一些数据,
但我不想将TextBox上的任何更改反映到组合框的ItemsSource
,所以我将绑定模式设置为OneTime,它工作正常,但我有一个清除内容的按钮单击文本框时,即使我从组合框中选择一个项目,它也会保持清晰:
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>
清除按钮:
private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtId.Text = txtPCount.Text = txtTitle.Text = "";
chkPblshd.IsChecked = false;
}
答案 0 :(得分:0)
使用绑定模式OneWay
代替OneTime
,因此模型中的更改将反映在用户界面中。
不幸的是,由于您手动设置Text
属性 ,因此这还不够。这会摧毁你的绑定!而是清除绑定值中的文本。