Du könntest einen Konverter schreiben und ihn im Binding angeben
(natürlich sinnvollere Namen wählen):
1 | using System;
|
2 | using System.Windows;
|
3 | using System.Windows.Data;
|
4 |
|
5 | public class MyConverter : IValueConverter
|
6 | {
|
7 | public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
8 | {
|
9 | return value != null;
|
10 | }
|
11 |
|
12 | public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
13 | {
|
14 | return DependencyProperty.UnsetValue;
|
15 | }
|
16 | }
|
1 | <Window.Resources>
|
2 | <local:MyConverter x:Key="NullToBooleanConverter" />
|
3 | </Window.Resources>
|
4 |
|
5 | ...
|
6 |
|
7 | <local:MyUserControl Height="100" Width="100" IsEnabled="{Binding Foo,Converter={StaticResource NullToBooleanConverter}}" />
|
Oder per Trigger:
1 | <local:MyUserControl Height="100" Width="100">
|
2 | <local:MyUserControl.Style>
|
3 | <Style TargetType="{x:Type local:MyUserControl}">
|
4 | <Setter Property="IsEnabled" Value="True" />
|
5 | <Style.Triggers>
|
6 | <DataTrigger Binding="{Binding Foo}" Value="{x:Null}">
|
7 | <Setter Property="IsEnabled" Value="False" />
|
8 | </DataTrigger>
|
9 | </Style.Triggers>
|
10 | </Style>
|
11 | </local:MyUserControl.Style>
|
12 | </local:MyUserControl>
|
Man könnte auch einfach ein Property implementieren und daran binden:
1 | public bool BlaEnabled
|
2 | {
|
3 | get { return Foo != null; }
|
4 | }
|
Und nicht vergessen, den DataContext des Fensters zu setzen.