我拥有的是一个具有IsReadOnly属性的对象。如果此属性为true,我想将按钮上的IsEnabled属性设置为false(例如)。

我愿意相信我可以像IsEnabled="{绑定路径=!IsReadOnly}”,但这与WPF不兼容。

我是否不得不经历所有的样式设置?对于将一个bool值设置为另一个bool值的倒数这样简单的事情来说,似乎太啰嗦了。

<Button.Style>
    <Style TargetType="{x:Type Button}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True">
                <Setter Property="IsEnabled" Value="False" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="False">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Button.Style>

当前回答

我遇到了一个倒置的问题,但是有一个简洁的解决方法。

动机是XAML设计器会显示一个空控件,例如当没有数据上下文/没有MyValues (itemssource)时。

初始代码:当MyValues为空时隐藏控件。 改进的代码:当MyValues为非空或空时显示控制。

当然,问题是如何表达“1个或多个项目”,这与0个项目相反。

<ListBox ItemsSource={Binding MyValues}">
  <ListBox.Style x:Uid="F404D7B2-B7D3-11E7-A5A7-97680265A416">
    <Style TargetType="{x:Type ListBox}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding MyValues.Count}">
          <Setter Property="Visibility" Value="Collapsed"/>
        </DataTrigger>
      </Style.Triggers>
    </Style>
  </ListBox.Style>
</ListBox>

我通过添加:

<DataTrigger Binding="{Binding MyValues.Count, FallbackValue=0, TargetNullValue=0}">

因此,为绑定设置默认值。当然,这并不适用于所有类型的逆问题,但帮助我用干净的代码。

其他回答

不知道这是否与XAML相关,但在我简单的Windows应用程序中,我手动创建了绑定,并添加了一个格式事件处理程序。

public FormMain() {
  InitializeComponent();

  Binding argBinding = new Binding("Enabled", uxCheckBoxArgsNull, "Checked", false, DataSourceUpdateMode.OnPropertyChanged);
  argBinding.Format += new ConvertEventHandler(Binding_Format_BooleanInverse);
  uxTextBoxArgs.DataBindings.Add(argBinding);
}

void Binding_Format_BooleanInverse(object sender, ConvertEventArgs e) {
  bool boolValue = (bool)e.Value;
  e.Value = !boolValue;
}

你考虑过IsNotReadOnly属性吗?如果被绑定的对象是MVVM域中的ViewModel,那么附加属性非常有意义。如果它是一个直接的实体模型,您可能会考虑组合并将实体的专用ViewModel呈现给表单。

在视图模型中再添加一个属性,它将返回反向值。 然后把它绑定到按钮上。 像;

视图模型:

public bool IsNotReadOnly{get{return !IsReadOnly;}}

在xaml:

IsEnabled="{Binding IsNotReadOnly"}

在@Paul的回答之后,我在ViewModel中写了以下内容:

public bool ShowAtView { get; set; }
public bool InvShowAtView { get { return !ShowAtView; } }

我希望在这里有一个片段可以帮助到一些人,可能是像我这样的新手。 如果有错误,请告诉我!

顺便说一句,我也同意@heltonbiker的评论——如果你不用使用它超过3次,这绝对是正确的方法……

I did something very similar. I created my property behind the scenes that enabled the selection of a combobox ONLY if it had finished searching for data. When my window first appears, it launches an async loaded command but I do not want the user to click on the combobox while it is still loading data (would be empty, then would be populated). So by default the property is false so I return the inverse in the getter. Then when I'm searching I set the property to true and back to false when complete.

private bool _isSearching;
public bool IsSearching
{
    get { return !_isSearching; }
    set
    {
        if(_isSearching != value)
        {
            _isSearching = value;
            OnPropertyChanged("IsSearching");
        }
    }
}

public CityViewModel()
{
    LoadedCommand = new DelegateCommandAsync(LoadCity, LoadCanExecute);
}

private async Task LoadCity(object pArg)
{
    IsSearching = true;

    //**Do your searching task here**

    IsSearching = false;
}

private bool LoadCanExecute(object pArg)
{
    return IsSearching;
}

然后,对于组合框,我可以直接绑定到IsSearching:

<ComboBox ItemsSource="{Binding Cities}" IsEnabled="{Binding IsSearching}" DisplayMemberPath="City" />