2017-09-05 63 views
0

我已經將複選框的Checked事件綁定到方法。我還通過一個轉換器將兩個Command參數傳遞給該方法(實現IMultiValueConverter)。在下面的代碼中,兩個CommandParameter綁定的類型分別爲boolstringWPF MultiBinding Converter對象類型

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="Checked"> 
       <i:InvokeCommandAction Command="{Binding IncludeCheckedCommand}"> 
        <i:InvokeCommandAction.CommandParameter> 
         <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked"> 
          <Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding> 
          <Binding Source="{StaticResource TopLeft}"></Binding> 
         </MultiBinding> 
        </i:InvokeCommandAction.CommandParameter> 
       </i:InvokeCommandAction> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
</CheckBox> 

爲什麼,如果我更換行:

<Binding ElementName="cbTopLeftHeader" Path="IsChecked"></Binding> 

有了:

<Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"></Binding> 

在我的轉換器從bool型器isChecked參數變化鍵入DependencyPropertyDependencyProperty.UnsetValue)?

我怎樣才能達到通過Checked屬性沒有通過名稱綁定到自己的元素?

+1

您沒有綁定CheckBox的CommandParamter屬性(例如[this](https://stackoverflow.com/q/7617375/1136211)),因此'RelativeSource Self'不是CheckBox。 – Clemens

回答

1

擺脫互動觸發,並使用CheckBoxCommandCommandParameter屬性:

<CheckBox Name="cbTopLeftHeader" Content="Top Left Header" Command="{Binding IncludeCheckedCommand}"> 
    <CheckBox.CommandParameter> 
     <MultiBinding Converter="{StaticResource MultiBindingConverter}" ConverterParameter="IncludeChecked"> 
      <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/> 
      <Binding Source="{StaticResource TopLeft}"></Binding> 
     </MultiBinding> 
    </CheckBox.CommandParameter> 
</CheckBox> 

或與您當前使用ElementName結合CheckBox辦法堅持下去。 InvokeCommandAction{RelativeSource Self}不是CheckBox

+0

謝謝你mm8。我沒有意識到(雖然現在看起來很明顯)默認情況下複選框的命令是'Checked'事件。您的回答和Clemens評論上面回答我的問題。 – Doug