我是当前的情景。 主类(控制代码隐藏):
public class MainClass : UserControl
{
public ObservableCollection<MyItem> ListItems
{
get { return (ObservableCollection<MyItem>)GetValue(ListItemsProperty); }
set { SetValue(ListItemsProperty, value); }
}
// Using a DependencyProperty as the backing store for ListContent. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ListItemsProperty =
DependencyProperty.Register("ListItems", typeof(ObservableCollection<MyItem>), typeof(MainClass), new PropertyMetadata(new ObservableCollection<MyItem>()));
}
My Type类:
public class MyItem: ContentControl
{
#region visual
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MyItem), new PropertyMetadata(""));
public string Path
{
get { return (string)GetValue(PathProperty); }
set { SetValue(PathProperty, value); }
}
// Using a DependencyProperty as the backing store for Path. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PathProperty =
DependencyProperty.Register("Path", typeof(string), typeof(MyItem), new PropertyMetadata(null));
#endregion
}
我的目标是直接在xaml中使用此集合,而wf和wft是使用控件的名称空间声明。 所以我写了一个测试页面,其中有一些属性直接在其中
<wf:MainClass x:Name="NewControl">
<wf:MainClass.ListItems>
<wft:MyItem Text="{Binding T, ElementName=page, FallbackValue=bad}"
Path="F1 M 38,54.2651C 43.2252,54.2651 48.1316,50.4139 50.2338,45.9886L 25.7662,45.9886C 27.8684,50.4139 32.7748,54.2651 38,54.2651 Z M 54.9656,39.6913C 56.9428,39.6913 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0245,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.6913 21.1591,39.6928L 21.1591,39.6913L 54.8409,39.6913L 54.9656,39.6913 Z M 26.9167,20.5833C 32.1634,20.5833 36.4167,24.8366 36.4167,30.0833C 36.4167,31.395 35.3533,32.4583 34.0417,32.4583C 32.73,32.4583 31.6667,31.395 31.6667,30.0833C 31.6667,27.46 29.54,25.3333 26.9167,25.3333C 24.2933,25.3333 22.1667,27.46 22.1667,30.0833C 22.1667,31.395 21.1033,32.4583 19.7917,32.4583C 18.48,32.4583 17.4167,31.395 17.4167,30.0833C 17.4167,24.8366 21.67,20.5833 26.9167,20.5833 Z M 49.0833,20.5833C 54.33,20.5833 58.5833,24.8366 58.5833,30.0833C 58.5833,31.395 57.52,32.4583 56.2083,32.4583C 54.8966,32.4583 53.8333,31.395 53.8333,30.0833C 53.8333,27.46 51.7067,25.3333 49.0833,25.3333C 46.46,25.3333 44.3333,27.46 44.3333,30.0833C 44.3333,31.395 43.27,32.4583 41.9583,32.4583C 40.6466,32.4583 39.5833,31.395 39.5833,30.0833C 39.5833,24.8366 43.8366,20.5833 49.0833,20.5833 Z " ></wft:MyItem>
<wft:MyItem Text="LESS TIME!"
Path="F1 M 38,23.75C 46.7445,23.75 53.8333,30.8388 53.8333,39.5833C 53.8333,48.3278 46.7445,55.4167 38,55.4167C 29.2555,55.4167 22.1667,48.3278 22.1667,39.5833C 22.1667,30.8388 29.2555,23.75 38,23.75 Z M 38,27.7083C 31.4416,27.7083 26.125,33.025 26.125,39.5833C 26.125,46.1417 31.4416,51.4583 38,51.4583C 44.5584,51.4583 49.875,46.1417 49.875,39.5833C 49.875,33.025 44.5584,27.7083 38,27.7083 Z M 37.6042,30.875C 38.4786,30.875 39.1875,31.5839 39.1875,32.4583L 39.1875,38.3959L 45.125,38.3959C 45.9994,38.3959 46.7083,39.1047 46.7083,39.9792L 46.7083,40.7708C 46.7083,41.6453 45.9994,42.3542 45.125,42.3542L 38,42.3542C 37.2083,42.2222 36.4167,42.0903 35.9548,41.6285C 35.493,41.1667 35.3611,40.375 35.2292,39.5833L 35.2292,32.4583C 35.2292,31.5839 35.938,30.875 36.8125,30.875L 37.6042,30.875 Z M 49.0833,20.5833C 52.5811,20.5833 55.4167,23.4189 55.4167,26.9167C 55.4167,28.4256 54.889,29.8113 54.008,30.8993C 51.9429,27.1006 48.5651,24.1189 44.4835,22.5632C 45.6379,21.3439 47.2718,20.5833 49.0833,20.5833 Z M 26.9167,20.5834C 28.7282,20.5834 30.3621,21.3439 31.5165,22.5632C 27.4349,24.1189 24.0571,27.1006 21.992,30.8993C 21.111,29.8113 20.5833,28.4256 20.5833,26.9167C 20.5833,23.4189 23.4189,20.5834 26.9167,20.5834 Z " ></wft:MyItem>
</wf:MainClass.ListItem>
</wf:MainClass>
在xaml MainClass页面中,我将listview的ItemSource绑定到ListItem observablecollection。 现在如果我在集合定义中尝试任何类型的数据绑定它都不起作用(如果我使用相同的数据绑定,它工作正常)。 在这个例子中,我在页面的代码后面(使用elementname“page”调用)声明了一个带有文本的名为“T”的属性。 然而,静态文本(如“LESS TIME !!”)工作正常。 奇怪的是绑定没有执行,我试图设置调试转换器,但系统从不调用它。 这是绑定不执行的方式(但后退值有效)。
注意:如果我以编程方式将内容添加到集合中,则可以正常工作。 例如:
<wf:MainClass x:Name="MainClassControl" BarListItems="{Binding ExampleBarListItem, ElementName=page}" />
其中ExampleBarListItem是ObservableCollection
谢谢
详细信息: 我试图将Text绑定到self对象,这是有效的。 例如:
<wft:MyItem x:Name="Pippo" Text="{Binding Name, FallbackValue=Bad, RelativeSource={RelativeSource Mode=Self}}"
Path="F1 M 38,54.2651C 43.2252,54.2651 48.1316,50.4139 50.2338,45.9886L 25.7662,45.9886C 27.8684,50.4139 32.7748,54.2651 38,54.2651 Z M 54.9656,39.6913C 56.9428,39.6913 58.1863,41.1792 58.5833,43.0379C 57.6384,52.7654 47.9756,61.75 38,61.75C 28.0245,61.75 18.3616,52.7654 17.4167,43.0378C 17.8137,41.1792 19.0572,39.6913 21.1591,39.6928L 21.1591,39.6913L 54.8409,39.6913L 54.9656,39.6913 Z M 26.9167,20.5833C 32.1634,20.5833 36.4167,24.8366 36.4167,30.0833C 36.4167,31.395 35.3533,32.4583 34.0417,32.4583C 32.73,32.4583 31.6667,31.395 31.6667,30.0833C 31.6667,27.46 29.54,25.3333 26.9167,25.3333C 24.2933,25.3333 22.1667,27.46 22.1667,30.0833C 22.1667,31.395 21.1033,32.4583 19.7917,32.4583C 18.48,32.4583 17.4167,31.395 17.4167,30.0833C 17.4167,24.8366 21.67,20.5833 26.9167,20.5833 Z M 49.0833,20.5833C 54.33,20.5833 58.5833,24.8366 58.5833,30.0833C 58.5833,31.395 57.52,32.4583 56.2083,32.4583C 54.8966,32.4583 53.8333,31.395 53.8333,30.0833C 53.8333,27.46 51.7067,25.3333 49.0833,25.3333C 46.46,25.3333 44.3333,27.46 44.3333,30.0833C 44.3333,31.395 43.27,32.4583 41.9583,32.4583C 40.6466,32.4583 39.5833,31.395 39.5833,30.0833C 39.5833,24.8366 43.8366,20.5833 49.0833,20.5833 Z " ></wft:MyItem>
此印刷品“Pippo”。 我不明白为什么来自elementname的常规绑定不起作用。
新也尝试使用datacontext,但没有。