数据触发器未触发

问题描述:

我有一个自定义用户控件,显示两个依赖项属性IsBusyBusyText数据触发器未触发

我要的是控制时IsBusy设置为true出现......这里的用户控件

<UserControl x:Class="MyNamespace.BusyDialog" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:controls="clr-namespace:MyNamespace" 
Height="Auto" 
Width="Auto" 
x:Name="busyControl"> 
<Grid Panel.ZIndex="10"> 
    <Border Opacity=".2"> 
     <Border.Background> 
      <RadialGradientBrush> 
       <GradientStop Color="#FF000000" Offset="0.59"/> 
       <GradientStop Color="#FFB6B6B6" Offset="0"/> 
      </RadialGradientBrush> 
     </Border.Background> 
    </Border> 
    <Border VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        BorderThickness="2" 
        BorderBrush="Gray">  
     <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, 
                   AncestorType=controls:BusyDialog}, 
                   Path=BusyText}" 
        Opacity="1" 
        Margin="20,10,20,10"/> 
    </Border> 
    <Grid.Style> 
     <Style TargetType="Grid"> 
      <Setter Property="Visibility" Value="Hidden"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType=controls:BusyDialog},Path=IsBusy}" Value="True"> 
        <Setter Property="Opacity" Value=".3" /> 
        <Setter Property="IsEnabled" Value="False" /> 
        <Setter Property="Visibility" Value="Visible"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Style> 
</Grid> 

这里的XAML是背后

public partial class BusyDialog : UserControl 
{ 
    #region Dependency Properties 

    public string BusyText 
    { 
     get { return (string)GetValue(BusyTextProperty); } 
     set { SetValue(BusyTextProperty, value); } 
    } 

    public bool IsBusy 
    { 
     get{ return (bool)GetValue(IsBusyProperty); } 
     set { SetValue(IsBusyProperty, value); } 
    } 

    public static readonly DependencyProperty IsBusyProperty = 
     DependencyProperty.Register(
      "IsBusy", 
      typeof(bool), 
      typeof(BusyControl), 
      new FrameworkPropertyMetadata(
       false, 
       FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

    public static readonly DependencyProperty BusyTextProperty = 
     DependencyProperty.Register(
      "BusyText", 
      typeof(string), 
      typeof(BusyControl), 
      new FrameworkPropertyMetadata(
       string.Empty, 
       FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 
    #endregion 

    public BusyDialog() 
    { 
     InitializeComponent(); 
    } 
} 
代码

这是我在我的视图中创建用户控件:

<localControls:BusyDialog x:Name="busyControl" 
           Grid.Row="0" 
           IsBusy="{Binding IsWorking}" 
           BusyText="{Binding WorkingText}"> 
</localControls:BusyDialog> 

我的代码有问题吗?每当我在我的ViewModel中设置IsWorking属性时,控件不会按照它应该显示的那样显示!

我也试着设置用户控制像这样绑定:

<UserControl x:Class="MyNamespace.BusyDialog" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:controls="clr-namespace:MyNamespace" 
Height="Auto" 
Width="Auto" 
x:Name="busyControl"> 
<Grid Panel.ZIndex="10"> 
    <Border Opacity=".2"> 
     <Border.Background> 
      <RadialGradientBrush> 
       <GradientStop Color="#FF000000" Offset="0.59"/> 
       <GradientStop Color="#FFB6B6B6" Offset="0"/> 
      </RadialGradientBrush> 
     </Border.Background> 
    </Border> 
    <Border VerticalAlignment="Center" 
        HorizontalAlignment="Center" 
        BorderThickness="2" 
        BorderBrush="Gray">  
     <TextBlock Text="{Binding ElementName=busyControl, Path=BusyText}" 
        Opacity="1" 
        Margin="20,10,20,10"/> 
    </Border> 
    <Grid.Style> 
     <Style TargetType="Grid"> 
      <Setter Property="Visibility" Value="Hidden"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding ElementName=busyControl,Path=IsBusy}" Value="True"> 
        <Setter Property="Opacity" Value=".3" /> 
        <Setter Property="IsEnabled" Value="False" /> 
        <Setter Property="Visibility" Value="Visible"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Grid.Style> 
</Grid> 

在触发绑定似乎怪我。试试这个:

<Trigger Property="IsBusy" Value="true"> 
    <Setter Property="Visibility" Value="Visible" /> 
    <Setter Property="Opacity" Value=".3" /> 
</Trigger> 

当我读你的代码时,逻辑对我来说并不完整。对于必须可见的控件来说,0.3的透明度足够了吗?请记住通过Controls XAML设置默认行为,并使用触发器仅修改适合该状态的值。

我想说的一个例子是触发器上的IsEnabled setter。这可能很好地在UserControl主设置中设置,因为当前控件只能在隐藏控件时才启用,当它显示为禁用时。

这是否有意义?希望这可以帮助!

您需要在视图模型中实现INotifyPropertyChanged。

public class WorkingViewModel : INotifyPropertyChanged 
{ 
    // ... 

    private bool _isWorking; 
    public bool IsWorking 
    { 
     get{ return _isWorking; } 
     set { 
       _isWorking = value; 
       RaisePropertyChanged("IsWorking"); 
      } 
    } 

    // ... 
    /// <summary> 
    /// Occurs when a property value changes. 
    /// </summary> 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string propertyName) 
    { 
     if(PopertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    // ... 
} 

这样,当您的IsWorking更改时,它将更新BusyDialog控件中的IsBusy。

<localControls:BusyDialog x:Name="busyControl" 
           Grid.Row="0" 
           IsBusy="{Binding IsWorking}" 
           BusyText="{Binding WorkingText}"> 
</localControls:BusyDialog> 

确保为您WorkingText做同样的,如果你打算改变这一点。希望有所帮助。