Converter is a Markup
If you want simplify your code it’s better to make a converter who is also a markup
Previous code for converter
1 2 3 4 5 6 7 8 9 10 11 12 |
public class BoolToVisivilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
Previous code for xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<Grid Margin="10,245,0,0" HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid.Resources> <local:BoolToVisivilityConverter x:Key="btvc" /> </Grid.Resources> <TextBox Width="772" Height="52" Text="{Binding Dossier, FallbackValue=xxxxx}" TextWrapping="Wrap" Visibility="{Binding IsVisible, Converter={StaticResource btvc}}" /> </Grid> |
And new code for converter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class BoolToVisivilityMarkupConverter : MarkupExtension, IValueConverter { public override object ProvideValue(IServiceProvider serviceProvider) => this; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? Visibility.Visible : Visibility.Collapsed; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
and xaml
1 2 3 4 5 6 7 8 |
<TextBox HorizontalAlignment="Left" Height="38" Margin="10,320,0,0" TextWrapping="Wrap" Text="{Binding Dossier, FallbackValue=xxxxx}" VerticalAlignment="Top" Width="772" Visibility="{Binding IsVisible, Converter={local:BoolToVisivilityMarkupConverter}}" /> |
see the difference
Categories: Non classé
Commentaires récents