I have a textbox and i want to set a Clr property from ViewModel to another property in ViewModel on Visibility Changed.
<TextBox Text="{Binding Property1}">
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="Visbility" Value="Collapsed">
<Trigger.Setters>
///Set Property1 = Property2
</Trigger.Setters>
</Trigger>
</Style>
</TextBox>
Is there any way to do this?
Use an intermediary DependencyProperty
and Right BindingMode
Combination to achieve your desired Result :
<TextBox Text="{Binding Property1}">
<TextBox.Style>
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="Visbility" Value="Collapsed">
<Trigger.Setters>
<Setter Property="Tag" Value="{Binding Property2}" />
<Setter Property="Tag" Value="{Binding Mode=OneWayToSource,Path=property1}" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
I'm assuming that you want to set the value of property2(which is also a CLR property) in property1, which will ultimately set the Text
Property of TextBox
.
There can be a way to do this. You bind the Visibility of the TextBox to a clr property of your VM and in the setter of the clr visibility property change the value of Property1
<TextBox Text="{Binding Property1}" Visibility="{Binding Visibility, Converter={StaticResource ResourceKey=boolConvertor}}"></TextBox>
and in the VM
public bool Visibility
{
get { return visibility; }
set
{
visibility = value;
if (visibility == false)
{
Property1 = Property2;
}
OnPropertyChanged();
}
}
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.