简体   繁体   中英

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; }
}

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.

我解决了使用标签而不是名称

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM