我有一个ChildWindow控件,里面有几个TextBox,还有一个Button。当我单击Button时,我想更改其中一个TextBox的“IsReadOnly”属性。在我的按钮单击事件处理程序中,所有TextBox对象都是“null”。
有人可以解释为什么会这样,以及我如何访问它们?
感谢您的帮助。
编辑:
<localCW:cwBase x:Class="Administration.cwAdd"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
xmlns:localCW="clr-namespace:Administration.ChildWindows"
xmlns:localCtrl="clr-namespace:Administration.Controls"
Width="480"
Height="460"
Title="Add"
Header="Add New"
OkButtonText="Save">
<Grid x:Name="LayoutRoot"
Margin="2">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="80" />
<RowDefinition Height="30" />
<RowDefinition Height="30" />
....
....
<TextBlock Text="Relative Path:"
Grid.Row="2"
Grid.Column="0"
Margin="0,0,0,5"
HorizontalAlignment="Right" />
<Grid Grid.Row="2"
Grid.Column="1"
Width="250"
HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<localCtrl:RelativeURLTextBox x:Name="tbxNewUrl"
Grid.Column="0"
Margin="5 0 0 5"
Width="215"
HorizontalAlignment="Left"
Text="{Binding RelativeURL, Mode=TwoWay}" />
<Button Grid.Column="1"
Margin="3"
Width="24"
Height="24"
Click="btnEditRelURL_Click">
<Button.Content>
<Image Source="../images/edit_16px.png"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</Button.Content>
</Button>
</Grid>
private void btnEditRelURL_Click(object sender, RoutedEventArgs e)
{
// Neither of these worked vvv
tbxNewUrl.SetReadOnly(false);
//this.Dispatcher.BeginInvoke ( () => {tbxNewUrl.SetReadOnly(false);});
}
答案 0 :(得分:0)
看来问题是因为我试图访问基类中定义的控件,而不是我的派生类。我向基类添加了一个方法,以根据名称返回元素,并且它有效。 谢谢你的帮助。