在这种情况下,如何绑定到收集项目?
//Both collections have OnPropertyChanged("");
public ObservableCollection<Grid> ConnectorsGrids { get; set; }
public ObservableCollection<double> Connectors { get; set; }
//Coordinates collection
Connectors = new ObservableCollection<double>() { 10 , A - 20 };
ConnectorsGrids = new ObservableCollection<Grid>();
foreach (var e in Connectors)
{
ConnectorsGrids.Add(DrawConnector(new Thickness(e * YB1, 0, 0, 0)));
}
YB1
是元素大小的系数。它是可变的,取决于屏幕的大小
YB1
不在任何集合中。是类中的参数
DrawConnector()
将边距作为参数
我希望页边距根据屏幕尺寸而改变
目前,它会计算输出值,然后在更改屏幕大小时,更改仍会保留
答案 0 :(得分:0)
您可以尝试以下方法:
xaml:
<ItemsControl x:Name="PART_ItemsControl"
ItemsSource="{Binding Connectors}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Converter={local:ToGridConverter}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
转换器:
sealed class ToGridConverter : MarkupExtension, IValueConverter{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is double)
{
double e = (double)value;
return DrawConnector(new Thickness(e * YB1, 0, 0, 0));
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private ToGridConverter _converter;
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (_converter == null)
{
_converter = new ToGridConverter ();
}
return _converter;
}}