David
David

Reputation: 2556

Reuse element defined further up in App.xaml

I declare several converters in my App.xaml as follows but I am repeating myself which I really would like to avoid:

<c:ConverterChain x:Key="IsNotNull">
  <c:IsNullConverter />
  <c:InvertBoolConverter />
</c:ConverterChain>

<c:ConverterChain x:Key="HideWhenNull">
  <c:IsNullConverter />
  <c:InvertBoolConverter />
  <c:BoolToFromVisibilityConverter FalseEquivalent="Hidden" />
</c:ConverterChain>

<c:ConverterChain x:Key="CollapseWhenNull">
  <c:IsNullConverter />
  <c:InvertBoolConverter />
  <c:BoolToFromVisibilityConverter FalseEquivalent="Collapsed" />
</c:ConverterChain>

As you can see the IsNotNull could be reused in the two following converter chains, but is it possible to declare that somehow? I'm thinking of something like:

<c:ConverterChain x:Key="HideWhenNull">
  <Reference Name="IsNotNull" />
  <c:BoolToFromVisibilityConverter FalseEquivalent="Hidden" />
</c:ConverterChain>

Is there some WPF concept out there to cover this use case?

Upvotes: 0

Views: 50

Answers (2)

David
David

Reputation: 2556

Inspired by Khyads answer I opted for StaticResource instead of DynamicResource because there is no need for the extra overhead Dynamic implies and it works just fine with a static resource:

<c:ConverterChain x:Key="HideWhenNull">
  <StaticResource ResourceKey="IsNotNull" />
  <c:BoolToFromVisibilityConverter FalseEquivalent="Hidden" />
</c:ConverterChain>

Upvotes: 0

Nathan Kovner
Nathan Kovner

Reputation: 2633

<c:ConverterChain x:Key="HideWhenNull">
  <DynamicResource  ResourceKey="IsNotNull"/>
  <c:BoolToFromVisibilityConverter FalseEquivalent="Hidden" />
</c:ConverterChain>

As long as the ConverterChain class supports nesting.

Upvotes: 1

Related Questions