简体   繁体   English

WPF窗口不会通过属性绑定更新

[英]WPF window doesn't get updated by property binding

I have a WPF form with a textbox which is defined in xaml file as follows: 我有一个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">

on the xaml.cs file I have the following code: 在xaml.cs文件上,我具有以下代码:

private void someBtn_Click(object sender, RoutedEventArgs e)
{
    ...
    ToolDataContext.ItemInstance.IsToShow = true;
    ...
}

In the Item class I have the following code for the property IsToShow: 在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));
    }

    ...
}

I would expect the window to show the textbox when I click when I on the button. 我希望当我在按钮上单击时,窗口会显示文本框。 But it doesn't happen. 但这不会发生。

Can anyone give me a lead about what am I doing wrong? 谁能给我一个有关我做错了什么的线索?

尝试将Path添加到数据触发器绑定

<DataTrigger Binding="{Binding Path=ToolDataContext.ItemInstance.IsToShow, Mode=TwoWay}" Value="True">
<DataTrigger Binding="{Binding ToolDataContext.ItemInstance.IsToShow, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="True">

假设您已分配数据上下文,则需要添加UpdateSourceTrigger = PropertyChanged

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM