简体   繁体   中英

DataTrigger binding to a get-only property

Is it possible to bind a property with no setter to a DataTrigger in WPF? I have the following code and the property's getter is not called as expected:

<TextBlock>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsLoggedIn}" Value="True">
                    <Setter Property="Text" Value="Connected" />
                    <Setter Property="Foreground" Value="Green" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsLoggedIn}" Value="False">
                    <Setter Property="Text" Value="Disconnected" />
                    <Setter Property="Foreground" Value="Black" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

public bool IsLoggedIn
{
    get { return Connection.IsLoggedIn; }
}

INPC is implemented properly (though I don't think that actually matters in this situation) and properties with setters behave properly. Is it possible to bind this property to a DataTrigger (and have it update properly) as it stands now?

Its possible. Why not?

Its all fine with having just a getter on the source side of binding. Target of the binding must be a dependency property but the source can be anything.

If you wish changes to be fired properly you will have to fire PropertyChanged with property name IsLoggedIn too once Connection.IsLoggedIn gets changed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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