WPF IsEnabled 网格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20746400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:17:14  来源:igfitidea点击:

WPF IsEnabled Grid

c#.netwpfpropertiesisenabled

提问by zak zak

I want to get my grid disabled when my property is Null and enabled when is Not null.

我想在我的属性为 Null 时禁用我的网格并在 Not null 时启用。

In my .XAML :

在我的 .XAML 中:

  <Grid Grid.Row="2" IsEnabled="{Binding ElementName=dgCustomers, Path=SelectedItem}">
      <my:ProductsHistoryDetailView />
  </Grid>

In my ViewModel.cs :

在我的 ViewModel.cs 中:

    public ProductHistory SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            if (_SelectedItem != value)
            {
                _SelectedItem = value;
                RaisePropertyChanged(() => SelectedItem);
            }
        }
    }

回答by Loetn

You should add an extra property to your viewmodel.

您应该为您的视图模型添加一个额外的属性。

    public bool IsGridEnabled
    {
        get 
          { 
             return this.SelectedItem != null;
          }
    }

<Grid Grid.Row="2" IsEnabled="{Binding IsGridEnabled}">
     <my:ProductsHistoryDetailView />
</Grid>

And when your SelectedItemchanges, call the OnPropertyChanged event for IsGridEnabled:

当您SelectedItem更改时,调用 OnPropertyChanged 事件IsGridEnabled

public ProductHistory SelectedItem
{
    get { return _SelectedItem; }
    set
    {
        if (_SelectedItem != value)
        {
            _SelectedItem = value;
            RaisePropertyChanged(() => SelectedItem);
            RaisePropertyChanged(() => IsGridEnabled);
        }
    }
}

回答by NebulaSleuth

Use a Style Trigger to change the Enabled Property instead of trying to bind it directly

使用样式触发器更改 Enabled 属性,而不是尝试直接绑定它

<Grid Grid.Row="2">
    <Grid.Style>
        <Style TargetType="Grid">
           <Setter Property="IsEnabled" Value="True"/>
           <Style.Triggers>
               <DataTrigger Binding="{Binding ElementName=dgCustomers, Path=SelectedItem"}" Value={x:Null}>
                    <Setter Property="IsEnabled" Value="False"/>
               </DataTrigger>
           </Style.Triggers>            
        </Style>
    </Grid.Style>
</Grid>

回答by McGarnagle

You could use an IValueConverter:

你可以使用一个IValueConverter

<Grid Grid.Row="2" IsEnabled="{Binding 
    ElementName=dgCustomers, Path=SelectedItem, 
    Converter={StaticResource NullToFalseConverter}">

public class NullToFalseConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == null ? false : true;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Alternatively, it might be easier to add another property IsSelectedto your view model, which you could bind directly to IsEnabled:

或者,将另一个属性添加IsSelected到您的视图模型可能会更容易,您可以直接绑定到IsEnabled

public bool IsSelected
{
    get { return _isSelected; }
    set
    {
        if (_isSelected != value)
        {
            _isSelected = value;
            RaisePropertyChanged(() => IsSelected);
        }
    }
}

public ProductHistory SelectedItem
{
    get { return _SelectedItem; }
    set
    {
        if (_SelectedItem != value)
        {
            _SelectedItem = value;
            RaisePropertyChanged(() => SelectedItem);
        }
        IsSelected = value != null;
    }
}

回答by Ringo

Please make sure your selectebitem has a valid value like this:

请确保您的 selectebitem 具有如下有效值:

<Grid Grid.Row="2" IsEnabled="{Binding ElementName=dgCustomers, Path=SelectedItem.Value}">
      <my:ProductsHistoryDetailView />
  </Grid>