vendredi 28 avril 2023

Is it possible to set property we are binding a TextBox value to in WPF?

I have a checkbox and a textbox. When checkbox is unticked I would like the textbox to be disabled and cleared, so when the user hits save, an empty string should be saved to the property the textbox is bound to. Currently however only the content itself gets erased. Is there a way I can change my code to the property bound gets changed too? I would like the same effect uppon unchecking the checkbox, as if the user erased the textbox contents by hand.

<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
    <Setter Property="Text" Value="{Binding Path=Data.SomeValue, Mode=TwoWay}" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=SomeCheckBox, Path=IsChecked}" Value="False">
            <Setter Property="IsEnabled" Value="False" />
            <Setter Property="Text" Value="{Binding x:Null}" />
        </DataTrigger>
    </Style.Triggers>
</Style>
</TextBox.Style>



Aucun commentaire:

Enregistrer un commentaire