Page 1 of 1

Only a single instance of a CroppedBitmap visible in an ItemsControl

Posted: 04 Dec 2023, 17:42
by David Jefferies
We have an issue which we think is similar to this one:

viewtopic.php?t=1951

In our case the image is a CroppedBitmap from an atlas
 <CroppedBitmap x:Key="T_HUD_icon_button_KBD_mouse" Source="{StaticResource T_HUD_atlas_main}" SourceRect="573,977,64,64"/>
and we have a custom ContentControl with a style:
<Style x:Key="InputActionControl-Style" TargetType="{x:Type ctrl:InputActionControl}" BasedOn="{StaticResource BaseUnfocusable-Style}">
		<Setter Property="Template" Value="{StaticResource InputActionControl-Template}"/>
		<Setter Property="Content" Value="{x:Null}"/>
		<Style.Triggers>
			<Trigger Property="InputAssetPath" Value="Keyboard_Mouse2D">
				<Setter Property="Tag" Value="True"/>
				<Setter Property="Content">
					<Setter.Value>
						<Rectangle HorizontalAlignment="Center" Height="32" VerticalAlignment="Center" Width="32">
							<Rectangle.Fill>
								<ImageBrush ImageSource="{StaticResource T_HUD_icon_button_KBD_mouse}"/>
							</Rectangle.Fill>
						</Rectangle>
					</Setter.Value>
				</Setter>
			</Trigger>
...
...
		</Style.Triggers>
	</Style>
Only one instance of an image ever gets displayed in the ItemsControl

Is there a workaround for this use case?

Re: Only a single instance of a CroppedBitmap visible in an ItemsControl

Posted: 04 Dec 2023, 19:28
by sfernandez
Hi David,

The problem is that you are setting the Content from the Style, so the same Rectangle is being set on each of the items.
You must be getting the error message "Child already has a logical parent" for all the items but the first one.

I guess you want to define that Rectangle as part of the control template and just set the ImageSource as Content:
<ControlTemplate x:Key="InputActionControl-Template" TargetType="{x:Type ctrl:InputActionControl}">
  ...
  <Rectangle Height="32" Width="32">
    <Rectangle.Fill>
      <ImageBrush ImageSource="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
    </Rectangle.Fill>
  </Rectangle>
  ...
</ControlTemplate>
<Style x:Key="InputActionControl-Style" TargetType="{x:Type ctrl:InputActionControl}" BasedOn="{StaticResource BaseUnfocusable-Style}">
  <Setter Property="Template" Value="{StaticResource InputActionControl-Template}"/>
  ...
  <Style.Triggers>
    <Trigger Property="InputAssetPath" Value="Keyboard_Mouse2D">
      <Setter Property="Content"  Value="{StaticResource T_HUD_icon_button_KBD_mouse}"/>
    </Trigger>
    ...
  </Style.Triggers>
</Style>
Could that work for you?

Re: Only a single instance of a CroppedBitmap visible in an ItemsControl

Posted: 05 Dec 2023, 15:13
by David Jefferies
yes that works - thanks!