David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

Only a single instance of a CroppedBitmap visible in an ItemsControl

04 Dec 2023, 17:42

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?
 
User avatar
sfernandez
Site Admin
Posts: 3363
Joined: 22 Dec 2011, 19:20

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

04 Dec 2023, 19:28

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?
 
David Jefferies
Topic Author
Posts: 27
Joined: 14 Jan 2021, 17:09

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

05 Dec 2023, 15:13

yes that works - thanks!

Who is online

Users browsing this forum: Bing [Bot] and 3 guests