Здравствуйте .
Есть проблема. не могу понять,почему она возникает.
У меня есть таб контрол. для итемов есть шаблон,вот он:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
<TabControl.ContentTemplate>
<DataTemplate>
<Grid Loaded="Grid_Loaded_1">
<Grid.RowDefinitions>
<RowDefinition Height="0*"/>
<RowDefinition Height="68*"/>
<RowDefinition Height="67*"/>
</Grid.RowDefinitions>
<ToolBarPanel Height="25" Focusable="False" VerticalAlignment="Top" Grid.RowSpan="2" Orientation="Horizontal">
<Button
Height="23"
Focusable="False"
VerticalAlignment="Top"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Width="25" IsEnabledChanged="Button_IsEnabledChanged"
Command="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=DataContext.SaveItemCommand}"
CommandParameter="SaveMagn1">
<Image
Height="23"
Width="20"
Source="/MagnitometerViewApplication;component/Save.png"
ToolTip="Сохранить магнитометр"/>
</Button>
<Button
Height="23"
Focusable="False"
VerticalAlignment="Top"
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Width="22"
Command="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=DataContext.DeleteItemCommand}"
CommandParameter="{Binding DataContext.SelectedProject.Current_Magn1, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}"
IsEnabledChanged="Button_IsEnabledChanged">
<Image Height="15" Width="16"
Source="/MagnitometerViewApplication;component/Minus.png"
ToolTip="Удалить магнитометр"/>
</Button>
<TextBox
Height="20"
Focusable="False"
Margin="9,3,0,0"
Style="{StaticResource {x:Static ToolBar.TextBoxStyleKey}}"
VerticalAlignment="Top"
IsReadOnly="True"
Background="#FFF1ECCB"
BorderBrush="#FF9C9981"
Text="{Binding Magnitometer.Commentary}"/>
<Separator
Height="25"
Margin="0"
VerticalAlignment="Top"
Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"
Width="10"
Background="{x:Null}"/>
<Button
Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
Focusable="False"
Background="{Binding UserSettings.Current_Brush1, Mode=OneWayToSource}"
Height="20"
VerticalAlignment="Top"
Width="20"
Command="{Binding ChangeColorCommand}"
CommandParameter="ColorChange1" Margin="0,3,0,0" BorderBrush="#FF818080"
ToolTip="Сменить цвет верхнего графика"
LayoutUpdated="ColorChange1_LayoutUpdated"/>
<Separator
Height="25"
Margin="0"
VerticalAlignment="Top"
Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}"
Width="12"
Background="{x:Null}"/>
<ComboBox
Height="20"
Focusable="False"
Margin="0,3,0,0"
Style="{StaticResource {x:Static ToolBar.ComboBoxStyleKey}}"
VerticalAlignment="Top"
Width="166"
ItemsSource="{Binding Settings.DataLists}"
SelectedValue="{Binding SelectedValueDataList,Mode=OneWayToSource}"
BorderThickness="1"
Loaded="ComboBox_Loaded_1"
Background="#FFF1ECCB"
BorderBrush="#FF9C9981"
ToolTip="Выбор данных"/>
<Separator Height="25" Margin="0" VerticalAlignment="Top" Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" Background="{x:Null}" Width="12"/>
<Label
Content="Глубина:"
Height="25"
VerticalAlignment="Top"
Focusable="False"/>
<TextBox
TextWrapping="Wrap"
Focusable="False"
Width="40"
Style="{StaticResource {x:Static ToolBar.TextBoxStyleKey}}"
Margin="0,3,0,2"
Text="{Binding Magnitometer.Depth, Mode=TwoWay, UpdateSourceTrigger=Explicit}"
PreviewKeyDown="Depth_PreviewKeyDown"
Background="#FFF1ECCB"
BorderBrush="#FF9C9981">
<TextBox.InputBindings>
<KeyBinding
Key="Enter"
Command="{Binding RelativeSource={RelativeSource AncestorType=Window,Mode=FindAncestor},Path=DataContext.CalculateCurrentCommand}"
CommandParameter="Magn1"/>
</TextBox.InputBindings>
</TextBox>
</ToolBarPanel>
<WindowsFormsHost Margin="0,25,2,0" Foreground="{x:Null}" Grid.Row="1" Grid.RowSpan="2"/>
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
Сам TabControl забиндин на коллекцию,которая с помощью команды заполняется новыми представлениями.
вот класс представления:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
public class ViewManager
{
private ProjectsView _view;
private ProjectsViewModel _pvm;
private UserControl1 _uc;
private Settings _userSet;
private Magnitometer _magn;
public string selectedStringItem;
public ICommand CloseFrameCommand
{
get { return _pvm.CloseFrameCommand; }
}
public ICommand OpenFrameCommand
{
get { return _pvm.OpenFrameCommand; }
}
public UserControl1 UserControl // это контрол с ZedGraph
{
get { return _uc; }
}
public Settings Settings
{
get { return _userSet; }
}
public Magnitometer Magnitometer
{
get { return _magn; }
}
public ViewManager(ProjectsView view, ProjectsViewModel pvm, Settings userSet, Magnitometer magn)
{
_view = view;
_pvm = pvm;
_uc = new UserControl1();
_userSet = userSet;
_magn = magn;
SelectedValueDataList = "Суммарный потенциал";
}
// вот тут забиндин ComboBox выбора записей
public String SelectedValueDataList
{
set
{
if (value == null)
{
return;
}
switch (value)
{
case "Суммарный потенциал":
_view.ColorChange1.Background = _userSet.ColorToBrushConverter(_userSet.ColorsList1[0]);
_uc.SetCurves(_magn.Name, value, _magn.Matrix.List_TP, _magn.Matrix.List_Date, 1, _userSet.ColorsList1[0]);
break;
case "Ток поляризации":
_view.ColorChange1.Background = _userSet.ColorToBrushConverter(_userSet.ColorsList1[1]);
_uc.SetCurves(_magn.Name, value, _magn.Matrix.List_AMP, _magn.Matrix.List_Date, 1, _userSet.ColorsList1[1]);
break;
case "Нижний датчик":
_view.ColorChange1.Background = _userSet.ColorToBrushConverter(_userSet.ColorsList1[2]);
_uc.SetCurves(_magn.Name, value, _magn.Matrix.List_LS, _magn.Matrix.List_Date, 1, _userSet.ColorsList1[2]);
break;
case "Верхний датчик":
_view.ColorChange1.Background = _userSet.ColorToBrushConverter(_userSet.ColorsList1[3]);
_uc.SetCurves(_magn.Name, value, _magn.Matrix.List_US, _magn.Matrix.List_Date, 1, _userSet.ColorsList1[3]);
break;
case "Ток(нижний датчик)":
_view.ColorChange1.Background = _userSet.ColorToBrushConverter(_userSet.ColorsList1[4]);
_uc.SetCurves(_magn.Name, value, _magn.Matrix.LSCurrent, _magn.Matrix.List_Date, 1, _userSet.ColorsList1[4]);
break;
}
}
}
}
вот команда:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
public void OpenFrame(object obj)
{
Magnitometer magn = (Magnitometer)obj
//новое представление,если оно уже есть в списке,то не добавляется
ViewManager vm = new ViewManager(
_view,
this,
this.UserSettings,
SelectedProject.List_Sm.First(item => item.GetRecordID == magn.GetRecordID));
// та самая коллекция
if(!managers.Any(item => item.Magnitometer.GetRecordID == vm.Magnitometer.GetRecordID))managers.Add(vm);
CheckItemCommandsState();
CheckProjectCommandsState(SelectedProject);
}
есть обработка события переключения между вкладками:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
private void Grid_Loaded_1(object sender, RoutedEventArgs e)
{
g = sender as Grid; //грид из которого потом берем WindowsFormsHost
}
private void ComboBox_Loaded_1(object sender, RoutedEventArgs e)
{
cb = sender as ComboBox; // тот контрол ,который выбирает записи
}
private void ProjectTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tc = sender as TabControl;
if (g != null)
{
((WindowsFormsHost)g.Children[1]).Child = ((ViewManager)tc.SelectedItem).UserControl; // заменяет Zedgraph из выбраного
}
}
Так вот в чем суть. Когда создаются новые вкладки, по сути не создаются новые контролы,а в них просто заменяется информация из выбранного представления. Так вот,когда я в одной вкладке меняю значение в ComboBox, меняется график, я переключаю на другую вкладку,но почему то ComboBox оставляет значение из предыдущей вкладки, хотя по идее, в разных вкладках ComboBox забиндин на разные SelectedValueDataList,потому что это разные объекты.
Но почему меняется в обоих,я не могу понять. То есть значение ComboBox сохраняется,а все остальные контролы меняют свое значение,выбранный график не соответствует выбранному до этого.
Как это побороть? Грубо говоря,как сохранить выбранные настройки в разных вкладках? я думал что если просто сделать несколько объектов,они будут сохранятся ,оказалось что не так все просто.