setting visibility of label in wpf
-
hi I am binding label through <Label Content="{Binding Path=OrderDetails.OrderPaymentId}" FontSize="20"> now i want if the value of OrderDetails.OrderPaymentId=0 then the visibility of label become collapsed please help how to do this using style or trigger
-
hi I am binding label through <Label Content="{Binding Path=OrderDetails.OrderPaymentId}" FontSize="20"> now i want if the value of OrderDetails.OrderPaymentId=0 then the visibility of label become collapsed please help how to do this using style or trigger
You need to create a value converter class, and bind the Visibility property to it in your xaml.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
You need to create a value converter class, and bind the Visibility property to it in your xaml.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
<Label.Style> <Style> <Style.Triggers> <DataTrigger Binding="{Binding Path=OrdeDetails.OrderPaymentId}" Value="0"> <Setter Property="Label.Visibility" Value="Collapsed"></Setter> </DataTrigger> </Style.Triggers> </Style> </Label.Style>
Or, like John said, write a value converter... 1) Create a class that inherits from IValueConverter 2) Implement the Convert function such that it returns Visibility.Collapsed for 0, or Visibility.Visible for anything else. 3) Create an instance of that class in your resources section in the XAML, and give it a key 4) Reference it in the binding:
{Binding OrderDetails.OrderPaymentId,Converter={StaticResource MyConverter}}
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
hi I am binding label through <Label Content="{Binding Path=OrderDetails.OrderPaymentId}" FontSize="20"> now i want if the value of OrderDetails.OrderPaymentId=0 then the visibility of label become collapsed please help how to do this using style or trigger
You can try both the Solution given above. Both are perfect answer.
Appreciate you for Voting Up my Articles & Forum posts. Mark as Answer if this post Answered your query.
Regards - Kunal Chowdhury | Software Developer | Blog | Twitter | Silverlight Tutorial | Indian Forum
-
Or, like John said, write a value converter... 1) Create a class that inherits from IValueConverter 2) Implement the Convert function such that it returns Visibility.Collapsed for 0, or Visibility.Visible for anything else. 3) Create an instance of that class in your resources section in the XAML, and give it a key 4) Reference it in the binding:
{Binding OrderDetails.OrderPaymentId,Converter={StaticResource MyConverter}}
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Converter works:
<UserControl.Resources>
<BooleanToVisibilityConverter x:Key="MyBooleanToVisibilityConverter" />
</UserControl.Resources>...
<Button Name="btnSomeButton" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0" Click="btnSomeButton_Click" Width="40px" Height="40px" Visibility="{Binding Path=bIsVisible, ElementName=SomeControl, Mode=TwoWay}" >
<Image Source="..\Icons\SomeIcon.jpeg" ToolTip="SomeButton" />
</Button>In above example, bIsVisible is a simple CLR property of type Boolean. But as with any other "Enum", you do NOT need a converter, you could have binded a property of type "System.Windows.Visibility" directly to your control:
<Button Name="btnSomeCmd" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="3" Click="btnSomeCommand_Click" Width="40px" Height="40px" Visibility="{Binding Path=SomeButtonVisibility, ElementName=SomeControl, Mode=TwoWay, diagnostics:PresentationTraceSources.TraceLevel=High}" >
<Image Source="..\Icons\...; ToolTip="SomeCommand" />
</Button>In above example, SomeButtonVisibility is a dependency property of type System.Windows.Visibility. I used a Dependency Property as supposed to a common CLR property only because I want take advantage of "Two Way" notification.
dev