9

TextBox2 つの基準を満たすをバインドする必要があります。

  • Text.Length > 0 の場合は IsEnabled
  • user.IsEnabled の場合は IsEnabled

user.IsEnabledデータ ソースから取得される場所。誰かがこれを行う簡単な方法を持っているかどうか疑問に思っていました。

XAML は次のとおりです。

<ContentControl IsEnabled="{Binding Path=Enabled, Source={StaticResource UserInfo}}"> 
    <TextBox DataContext="{DynamicResource UserInfo}" Text="{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding Path=Text, RelativeSource={RelativeSource Self}, Converter={StaticResource LengthToBool}}"/> 
</ContentControl>
4

3 に答える 3

7

GazTheDestroyer が言ったように、MultiBinding を使用できます。

MultiDataTrigger を使用して、XAML のみのソリューションでこれを達成することもできます。

ただし、条件を切り替える必要があるため、トリガーは等価のみをサポートします

<Style.Triggers>  
  <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text.Length}" Value="0" />
          <Condition Binding="{Binding Source=... Path=IsEnabled}" Value="False" />
        </MultiDataTrigger.Conditions>
        <Setter Property="IsEnabled" Value="False" />
      </MultiDataTrigger>  
</Style.Triggers>

条件のいずれかが満たされない場合、値はデフォルトまたはスタイルの値に設定されます。ただし、スタイルとトリガーの値をオーバーライドするため、ローカル値を設定しないでください。

于 2012-09-06T20:02:48.590 に答える
6

必要なのは論理的な のみであるためOR、各プロパティに対して 2 つの Triggers が必要です。

この XAML を試してください:

<StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=InputText, Path=Text}" Value="" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=MyIsEnabled}" Value="False" >
                        <Setter Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Resources>
        <StackPanel Orientation="Horizontal">
            <Label>MyIsEnabled</Label>
            <CheckBox IsChecked="{Binding Path=MyIsEnabled}" />
        </StackPanel>
        <TextBox Name="InputText">A block of text.</TextBox>
        <Button Name="TheButton" Content="A big button.">     
        </Button>
    </StackPanel>

と呼ばれるクラスに設定DataContextしました。明らかに、特定の.WindowDependencyPropertyMyIsEnabledDataContext

関連するコード ビハインドは次のとおりです。

public bool MyIsEnabled
{
    get { return (bool)GetValue(IsEnabledProperty); }
    set { SetValue(IsEnabledProperty, value); }
}

public static readonly DependencyProperty MyIsEnabledProperty =
    DependencyProperty.Register("MyIsEnabled", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(true));


public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
}

それが役立つことを願っています!

于 2012-09-06T20:24:09.040 に答える