For my project I am going to have some lines drawn, and I need to check if they will be in the Visible X and Y - so I thought I'd use a multi value converter that would return the line if it is inside the X,Y else return null or something. I want to know how to bind the X1, X2, etc of the line to the returned line object? In the following XAML you can see I try to pass the Lines observable collection with the visible min max etc, and I would like to see if the last line is within the boundary. If so return it (currently all of them return for testing purposes), and I want to bind the Line with the returned line. Wondering how to go about this...
The goal for the entire program is to eventually map the path a little robot car took - using these lines. And eventually I wanted some sort of zoom capabilities so I am trying to create the lines via binding.
My XAML items control looks like
<ItemsControl x:Name="Items">
<ItemsControl.ItemsSource>
<MultiBinding Converter="{StaticResource conv}">
<Binding Path="Lines"/>
<Binding Path="VisibleMinX"/>
<Binding Path="VisibleMinY"/>
<Binding Path="VisibleMaxX"/>
<Binding Path="VisibleMaxY"/>
</MultiBinding>
</ItemsControl.ItemsSource>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Line X1="{Binding X1}" X2="{Binding X2}" Y1="{Binding Y1}" Y2="{Binding Y2}" Stroke="Black" StrokeThickness="2"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My View model (code behind of main is empty)
class VM : INotifyPropertyChanged
{
public AddLineCommand addlinecommand { get; set; }
public VM()
{
VisibleMinX = 0;
VisibleMinY = 0;
VisibleMaxX = 300;
VisibleMaxY = 300;
//test
CreateLine(100, 100, 150, 150);
CreateLine(150, 150, 200, 280);
addlinecommand = new AddLineCommand(this);
}
private ObservableCollection<Line> lines = new ObservableCollection<Line>();
public ObservableCollection<Line> Lines
{
get
{
return lines;
}
}
public void CreateLine(int x1, int y1, int x2, int y2)
{
Line line = new Line();
line.X1 = x1;
line.Y1 = y1;
line.X2 = x2;
line.Y2 = y2;
line.StrokeThickness = 2;
SolidColorBrush black = new SolidColorBrush();
black.Color = Colors.Black;
line.Stroke = black;
Lines.Add(line);
OnPropertyChanged("Lines");
}
public void AddLine()
{
CreateLine(0, 0, 100, 100);
}
private int _VisibleMinX;
public int VisibleMinX
{
get { return _VisibleMinX; }
set { _VisibleMinX = value; }
}
private int _VisibleMaxX;
public int VisibleMaxX
{
get { return _VisibleMaxX; }
set { _VisibleMaxX = value; }
}
private int _VisibleMinY;
public int VisibleMinY
{
get { return _VisibleMinY; }
set { _VisibleMinY = value; }
}
private int _VisibleMaxY;
public int VisibleMaxY
{
get { return _VisibleMaxY; }
set { _VisibleMaxY = value; }
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
And my converter
class conv : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
ObservableCollection<Line> line = (ObservableCollection<Line>)values[0];
int MinX = (int)values[1];
int MinY = (int)values[2];
int MaxX = (int)values[3];
int MaxY = (int)values[4];
return line[line.Count-1]; //returning last line obj
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}