简体   繁体   English

如何将 XAML 中的 Canvas 元素绑定到代码隐藏中的 Canvas 属性?

[英]How to bind a Canvas element in XAML to a Canvas property in code behind?

I'm using C# and Silverlight for Windows Phone.我正在使用 C# 和 Silverlight 作为 Windows 电话。

I have a ListBox listBoxOfItemsA whose ItemsSource property is bound to an ObservableCollection observableCollectionOfItemsA.我有一个 ListBox listBoxOfItemsA,其 ItemsSource 属性绑定到 ObservableCollection observableCollectionOfItemsA。 Each TypeA item in the ListBox is drawn using a ListBox.ItemTemplate, as shown in the XAML. Notice the Canvas element. ListBox 中的每个 TypeA 项都是使用 ListBox.ItemTemplate 绘制的,如 XAML 中所示。请注意 Canvas 元素。 Without it, everything was fine.没有它,一切都很好。 Now, I have associated each instance of TypeA with 0 or more instances of TypeB.现在,我已将 TypeA 的每个实例与 0 个或多个 TypeB 实例相关联。 Each instance of TypeB defines a HighlightColor. TypeB 的每个实例都定义了一个 HighlightColor。 I need some way for each TypeA item in the ListBox to show the HighlightColor's of all TypeB items associated with it.我需要某种方式让 ListBox 中的每个 TypeA 项显示与其关联的所有 TypeB 项的 HighlightColor。 I was thinking of adding a thin Canvas element to each ListBox item, to divide it horizontally in 0 or more regions (0 would just leave the black background), and to paint in that Canvas as many rectangles as instances of TypeB are associated with that ListBox item (so that the total width of all those rectangles is constant).我正在考虑向每个 ListBox 项目添加一个薄的 Canvas 元素,将其水平划分为 0 个或多个区域(0 只会留下黑色背景),并在 Canvas 中绘制与 TypeB 实例相关联的矩形数量ListBox 项(以便所有这些矩形的总宽度保持不变)。

Question: What do I have to write at the "????"问题:我必须在“????”处写什么? position, so that I can bind the Canvas property (in TypeA) with the Canvas element in the ListBox.ItemTemplate? position,以便我可以将 Canvas 属性(在 TypeA 中)与 ListBox.ItemTemplate 中的 Canvas 元素绑定?

Thank you.谢谢你。

Edit 20120406_0202 : Each TypeA instance may be mapped to more than one TypeB instances at the same time .编辑 20120406_0202 :每个 TypeA 实例可以同时映射到多个 TypeB 实例。 For example: Each TypeA is a movement of money (from one account to another account, on a certain date, and belonging to a certain category, etc), and each TypeB is a set of conditions that each TypeA may or may not satisfy (for instance, its date is inside a certain range, its destination account is equal to a certain one, etc).例如:每个 TypeA 都是资金的流动(从一个账户到另一个账户,在某个日期,属于某个类别等),每个 TypeB 是一组条件,每个 TypeA 可能满足或不满足(例如,它的日期在某个范围内,它的目标帐户等于某个帐户,等等)。 For each TypeA, I want to show, at a glance, which sets of conditions it satisfies.对于每个 TypeA,我想一目了然地显示它满足哪些条件集。 Each set satisfied will show up with an additional piece of color.每个满意的套装都会显示出额外的颜色。

public class TypeA
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Canvas CanvasForItemsB {...}  // I would like something like this, here, and to bind it to the Canvas element in the XAML.
}

public class TypeB
{
    public SolidColorBrush HighlightColor {...}
}

ObservableCollection<TypeA> observableCollectionOfItemsA;
listBoxOfItemsA.ItemsSource  =observableCollectionOfItemsA;

<ListBox x:Name="listBoxOfItemsA">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Id, Mode=OneWay}" />
                <TextBlock Text="{Binding Name, Mode=OneWay}" />
                <Canvas ????="{Binding CanvasForItemsB, Mode=OneWay}" Width="480" Height="10" Background="Black" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

If you're trying to highlight the element(s) in your listbox, it'd probably be simpler to just place a Border around the StackPanel in your ItemTemplate and then set up a binding on the Border's background property.如果您试图突出显示列表框中的元素,可能更简单的做法是在 ItemTemplate 中的 StackPanel 周围放置一个 Border,然后在 Border 的背景属性上设置绑定。 Then you wouldn't have to worry about storing a bunch of Canvas objects in memory or having to inject them into your ListBox.这样您就不必担心将一堆 Canvas 对象存储在 memory 中或必须将它们注入您的列表框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在代码后面绑定到Canvas.Left或Canvas.Top附加属性? - How do I bind to Canvas.Left or Canvas.Top attached property in code behind? 是否可以在XAML中绑定Canvas的Children属性? - Is it possible to bind a Canvas's Children property in XAML? 如何绑定到XAML代码隐藏中定义的属性 - How to Bind to a property defined in code-behind from XAML 在后面的代码中更改 Canvas.Left 属性? - Change Canvas.Left property in code behind? 如何将XAML contentView绑定到其背后的代码 - How to bind XAML contentView to its code behind Windows Phone-如何在动态创建的画布中绘制形状-XAML与背后的代码 - Windows Phone - How to draw shapes in dynamically created canvas - XAML vs code behind 每次属性更改时,如何将XAML视图的属性绑定到背后的代码中的公共变量 - How to bind a property of a XAML view to a public variable in the code behind every time the property changes Xamarin.Forms:绑定到 XAML 中的代码隐藏属性 - Xamarin.Forms: bind to a code behind property in XAML 如果usercontrol的datacontext是视图模型,您如何绑定到xaml代码后面的属性? - How do you bind to a property in a xaml code-behind if the usercontrol's datacontext is a view-model? WPF:如何将Visibility属性绑定到xaml元素? - WPF: How to bind a Visibility property to an xaml element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM