Не так давно начал работать с WPF, поэтому не судите за карявость :)
Подскажите как правильно передать ObservableCollection из основной формы, на дополнительную. Передавал строковую, с ней так проходит. На главной форме есть DataGrid, и в нее, через ItemSourse все нормально приходит(Коллекция).
Код модуля UserControl
public partial class VDiagr : UserControl, INotifyPropertyChanged
{
public VDiagr()
{
Number2 = "";
InitializeComponent();
canvas = Canv;
}
public static readonly DependencyProperty Vectors_collection = DependencyProperty.Register("Vec2", typeof(ObservableCollection<Vectors>), typeof(VDiagr), new PropertyMetadata(new ObservableCollection<Vectors>(), OnPropertyChangedVector));
public ObservableCollection<Vectors> Vec2
{
get { return (ObservableCollection<Vectors>)this.GetValue(Vectors_collection); }
set { this.SetValue(Vectors_collection, value);
}
}
public class Vectors
{
public double angle { get; set; }
public SolidColorBrush brush { get; set; }
public double lenght { get; set; }
public string text { get; set; }
}
public static void OnPropertyChangedVector(DependencyObject sourse, DependencyPropertyChangedEventArgs e)
{
(sourse as VDiagr).ChangeVectors();
}
void ChangeVectors()
{
if (Vec2 != null)
{
Axis.Set(canvas, true, false, 5, false);
foreach (var item in Vec2)
{
Vect.SetRad(canvas, new V() { angle = item.angle, brush = item.brush, length = item.lenght, text = item.text });
}
}
}
}
Xaml основного
<Window
x:Class="Vec.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vdiagram="clr-namespace:VDiagram;assembly=VDiagram"
Title="MainWindow"
Width="470"
Height="470"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<StackPanel
Grid.Column="0"
Height="25"
VerticalAlignment="Top"
Orientation="Horizontal">
<TextBox
x:Name="tx"
Width="120"
Height="23"
Margin="3"
TextChanged="tx_TextChanged"
TextWrapping="Wrap" />
<Button
Width="75"
Margin="3"
Click="Button_Click_1"
Content="Button" />
</StackPanel>
<StackPanel Margin="0,30,0,0">
<DataGrid
x:Name="data_grid"
Height="200"
AutoGenerateColumns="True"
ItemsSource="{Binding vectors2, RelativeSource={RelativeSource AncestorType=Window}}" />
</StackPanel>
<vdiagram:VDiagr
Grid.Column="1"
Number2="{Binding Num2, RelativeSource={RelativeSource AncestorType=Window}}"
List_vectors="{Binding List_vectors,Mode=TwoWay,RelativeSource={RelativeSource
AncestorType=Window}}" />
</Grid>
</Window>
Код основной формы
public partial class MainWindow : Window, INotifyPropertyChanged {
public MainWindow()
{
InitializeComponent();
}
public class Vectors
{
public double angle { get; set; }
public SolidColorBrush brush { get; set; }
public double lenght { get; set; }
public string text { get; set; }
}
public ObservableCollection<Vectors> vectors1;
public ObservableCollection<Vectors> vectors2
{
set
{
vectors1 = value;
OnPropertyChanged(nameof(vectors2));
}
get
{
return vectors1;
}
}
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
vectors2 = new ObservableCollection<Vectors>
{
//Произвольное заполнение коллекции
};
}
}
DataContext = this;
Binding Num2, RelativeSource={RelativeSource AncestorType=Window}
на вот этоBinding Num2
.