1

我有一个自定义面板ListView。这些ListView's项目包含一个GroupBox. GroupBox包含ListView一个. 这个ListView's项目包含一个GroupBox等等。

上面的所有控件都有自定义模板和样式

Controls VisualTree 中有ToggleButtons最低节点。当这些按钮被选中时,我需要禁用除单击按钮之外的所有面板。

我想避免通过 viewModel 类中的父级进行事件链接。

我正在使用 mvvm 模式,如果可能的话,我想在 xaml 端解决它。

编辑:这是一个屏幕截图,Pick 按钮应该禁用面板

在此处输入图像描述

任何建议都受到热烈欢迎。

4

2 回答 2

1

您需要实现如下所示的相对源绑定。

IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=IsEnabled}"
于 2012-08-10T12:09:09.903 回答
0

Just have a read-only property in your ViewModel that is the negation of the property that your ToggleButton is bound to.

ViewModel:

private Boolean mSourceIsPicked;
public Boolean SourceIsPicked
{
    get { return mSourceIsPicked; }
    set 
    { 
        SetProperty("SourceIsPicked", ref mSourceIsPicked, value); 
        NotifyPropertyChanged("IsSourceChangeable");
    }
}

public Boolean IsSourceChangeable
{
    get { return ! this.SourceIsPicked; }
}

Then, in your View, just bind the IsEnabled property of the other controls to that new property.

<ComboBox ItemsSource="{Binding SourceTypes}"
          IsEnabled={Binding IsSourceChangeable}" />

The advantage of binding to a property is that you can add/remove controls in your view and just bind to this property without changing additional XAML. You can also change the behavior of any control by not binding to this property.

If you really want a XAML-only solution, you can name each of the controls in the panel, and use a DataTrigger using TargetName on the "SourceIsPicked" property to disable the others:

<ComboBox x:Name="cboSourceTypes" ... />
<ComboBox x:Name="cboSourceNames" ... />
<ToggleButton>
    <ToggleButton.Style>
        <Style TargetType="{x:Type ToggleButton}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding SourceIsPicked}" Value="True">
                    <Setter TargetName="cboSourceTypes" 
                            Property="IsEnabled" 
                            Value="False" />
                    <Setter TargetName="cboSourceNames" 
                            Property="IsEnabled" 
                            Value="False" />
                </DataTrigger>
            </Style.Triggers>
         </Style>
    </ToggleButton.Style>
</ToggleButton>

Note that this is all freehand, so you may need to adjust it a bit, but it gives you the idea.

于 2012-08-10T13:11:47.190 回答