2016-10-10 69 views
0

我想創建一個MultiDataTrigger。在其中一個條件下,我需要使用轉換器。由於它僅用於那裏,我不想將轉換器定義爲資源,但只能在本地使用。在MultiDataTrigger中本地使用IValueConverter

所以我做了這個MultiDataTrigger:

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition Binding="Property1" Value="Value1" /> 
     <Condition ... /> 
    </MultiDataTrigger.Conditions> 
    <Setter Property="SomeProp" Value="SomeVal"/> 
</MultiDataTrigger> 

這種結合避免定義轉換爲資源(基於https://stackoverflow.com/a/2304330/5333340):

<Binding Path="Property2"> 
    <Binding.Converter> 
     <converterNamespace:MyConverter/> 
    </Binding.Converter> 
</Binding> 

,並把兩件一起:

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition Binding="Property1" Value="Value1" /> 
     <Condition Value="Value2"> 
      <Binding Path="Property2">     <!-- Compiler --> 
       <Binding.Converter>     <!-- does  --> 
        <converterNamespace:MyConverter/> <!-- not  --> 
       </Binding.Converter>     <!-- like  --> 
      </Binding>         <!-- this! --> 
     </Condition> 
    </MultiDataTrigger.Conditions> 
    <Setter Property="SomeProp" Value="SomeVal"/> 
</MultiDataTrigger> 

但編譯器抱怨:「類型'條件'不支持直接內容「。 (原文:「Der Typ'Condition'unterstütztkeine direkten Inhalte。」)

有沒有辦法讓MultiDataTrigger工作而無需將轉換器定義爲資源?

回答

2

您的XAML缺少<Condition.Binding>標記。除此之外,Binding="{Property1}"也是無效的。

<MultiDataTrigger> 
    <MultiDataTrigger.Conditions> 
     <Condition Binding="{Binding Property1}" Value="Value1" /> 
     <Condition Value="Value2"> 
      <Condition.Binding>     <!-- here --> 
       <Binding Path="Property2"> 
        <Binding.Converter> 
         <converterNamespace:MyConverter/> 
        </Binding.Converter> 
       </Binding> 
      </Condition.Binding> 
     </Condition> 
    </MultiDataTrigger.Conditions> 
</MultiDataTrigger>