我有一个带有文本框的WPF表单,该文本框在xaml文件中定义如下:
<TextBox Grid.Column="1" Grid.Row="9" TabIndex="0" x:Name="txtboxExample" Width="170" >
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Visibility" Value="Hidden" />
<Style.Triggers>
<DataTrigger Binding="{Binding ToolDataContext.ItemInstance.IsToShow, Mode=TwoWay}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
<TextBox.Text>
<Binding Path="ToolDataContext.ItemInstance.UserText" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>
</TextBox>
...
<Button Click="someBtn_Click" Content="{x:Static res:Strings.ButtonString}" Name="someButton">
在xaml.cs文件中我有以下代码:
private void someBtn_Click(object sender, RoutedEventArgs e)
{
...
ToolDataContext.ItemInstance.IsToShow = true;
...
}
在Item类中,我有以下代码用于属性IsToShow:
public class Item : SyncableObject, ISearchableObject, INotifyPropertyChanged
{
...
private bool _isToShow;
public bool IsToShow
{
get { return _isToShow; }
set
{
if (value == _isToShow)
return;
_isToShow = value;
this.OnPropertyChanged("IsToShow");
}
}
...
new public event PropertyChangedEventHandler PropertyChanged;
new public void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
...
}
当我点击按钮时,我希望窗口显示文本框。 但它不会发生。
任何人都可以告诉我我做错了什么吗?
答案 0 :(得分:0)
尝试将Path添加到数据触发器绑定
<DataTrigger Binding="{Binding Path=ToolDataContext.ItemInstance.IsToShow, Mode=TwoWay}" Value="True">
答案 1 :(得分:0)
<DataTrigger Binding="{Binding ToolDataContext.ItemInstance.IsToShow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
假设您已分配了datacontext,则需要添加UpdateSourceTrigger = PropertyChanged