【发布时间】:2017-06-27 14:57:53
【问题描述】:
我的 xaml 中有一个用于从数据源中提取的 ID 字段的文本框。我希望这个值是只读的,这样用户就不能改变这个值。我有一个按钮供用户添加一个新对象,这当然需要一个 ID。我的对象“IsNew”上有一个布尔属性,当用户单击“添加新”按钮时,该属性设置为 true。单击该按钮时,我希望此文本框可编辑。所以基本上,当“IsNew = true”时。我怎样才能做到这一点?
//My Xaml for the textbox:
<TextBox x:Name="ID"
Text="{Binding SelectedRecord.ID}"/>
//Xaml for the button
<Button x:Name="AddNewRecordButton"
Grid.Column="1"
Margin="20,0,5,0"
HorizontalAlignment="Left"
VerticalAlignment="Bottom"
HorizontalContentAlignment="Left"
Height="24"
Width="90"
Command="{Binding AddNewRecordCommand}"
CommandParameter="{Binding ElementName=window}"/>
//Code for the command method
public void AddNewRecord(object parameter)
{
var newRecord = new StockingReason();
Records.Add(newRecord);
SelectedRecord = newRecord;
newRecord.IsNew = true;
var control = parameter as IFocusable;
control?.SetFocus();
}
【问题讨论】:
-
可以只做一个快速的数据触发器,或者直接绑定到纯 xaml 中的切换按钮 IsChecked 属性,或者像下面显示的彼得这样的值转换器。有多种方法可以做到这一点。
-
编辑了我的答案
-
请随意为这两个答案投票