Parni
Parni

Reputation: 141

Programmatically Binding Name ItemsControl

I create programmatically a grid of rectangles and each rectangle has a Label inside it. I detect which rectangle has been clicked using Event Command. My problem is that if I try to bind the rectangle Name I get this error: MarkupExtensions are not allowed for Uid or Name property values, so '{Binding ID}' is not valid So I can retrive the name only if I click on the Label. Is there a solution for this problem? My XAML:

<ItemsControl ItemsSource="{Binding CaskList}" HorizontalAlignment="Left" VerticalAlignment="Top">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas Width="1680" Height="800">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="MouseLeftButtonDown" >
                                    <cmd:EventToCommand Command="{Binding SelCaskCommand}" PassEventArgsToCommand="True" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Canvas>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="ContentPresenter">
                        <Setter Property="Canvas.Left" Value="{Binding Left}"/>
                        <Setter Property="Canvas.Top" Value="{Binding Top}"/>
                    </Style>
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Rectangle Stroke="Black" Width="64" Height="64" Fill="{Binding Color}" ></Rectangle>
                            <Label Content="{Binding ID}" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

I tried to bind the rectanle simply using

<Rectangle Stroke="Black" Width="64" Height="64" Fill="{Binding Color}" Name="{Binding ID}">

The class binded to Itemscontrol is

public class Cask
{
    public string ID { get; set; }
    public Int64 Left { get; set; }
    public Int64 Top { get; set; }
    public SolidColorBrush Color { get; set; }
}

Upvotes: 2

Views: 4314

Answers (2)

Suresh Kumar Veluswamy
Suresh Kumar Veluswamy

Reputation: 4363

It is not advisable to data binding the Name property. If you bind the name property, then the name becomes dynamic. If names are dynamic, then you cannot refer to the control from the code-behind. Hence you got the error " MarkupExtensions are not allowed for Uid or Name property values, so '{Binding ID}' is not valid".

If you really want to bind the ID property and use it either in your code behind or View model, then you can bind it to the Tag property.

Upvotes: 8

Parni
Parni

Reputation: 141

I solved using Tag instead of Name

Upvotes: 3

Related Questions