小编Par*_*rni的帖子

以编程方式绑定名称 ItemsControl

我以编程方式创建一个矩形网格,每个矩形内部都有一个标签。我使用事件命令检测哪个矩形被单击。我的问题是,如果我尝试绑定矩形名称,则会收到此错误: Uid 或名称属性值不允许使用 MarkupExtensions,因此“{Binding ID}”无效 因此,只有单击标签。这个问题有解决办法吗?我的 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>
Run Code Online (Sandbox Code Playgroud)

我尝试简单地使用绑定矩形

<Rectangle Stroke="Black" Width="64" Height="64" Fill="{Binding Color}" Name="{Binding ID}">
Run Code Online (Sandbox Code Playgroud)

绑定到 Itemscontrol 的类是

public class Cask
{ …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding itemscontrol

2
推荐指数
1
解决办法
3999
查看次数

标签 统计

binding ×1

c# ×1

itemscontrol ×1

wpf ×1