IsSynchronizedWithCurrentItem="True"
ListBox
. , ListBox.SelectedItem
ListBox.Items.CurrentItem
. , ListBox.Items.CurrentItem
ICollectionView.CurrentItem
( CollectionViewSource.GetDefaultView(Schemas)
). , Schemas
, CurrentItem
, CurrentItem
( , , null
, ).
, ListBox.SelectedItem
, , RaisePropertyChangedEvent(nameof(ActiveSchema))
, , , ActiveSchema
. , , , . , CurrentItem
Schemas
, . , IsActive = false
, "" Schemas
, , , CurrentItem
ListBox.SelectedItem
. , ActiveSchema
. , ActiveSchema
( ) , ( , ).
:
# 1
IsSynchronizedWithCurrentItem="False"
ListBox
( ). . - , .
# 2
ActiveSchema
:
bool ignoreActiveSchemaChanges = false;
public IPowerSchema ActiveSchema
{
get { return pwrManager.CurrentSchema; }
set
{
if (ignoreActiveSchemaChanges) return;
if (value != null && !value.IsActive)
{
ignoreActiveSchemaChanges = true;
pwrManager.SetPowerSchema(value);
ignoreActiveSchemaChanges = false;
}
}
}
, CurrentItem
, ActiveSchema
.
# 3
CurrentItem
, "" . MainWindowViewModel.Schemas
, setNewCurrSchema
. :
PowerManager
:
private void setNewCurrSchema(IPowerSchema newActiveSchema, Action action = null)
{
var oldActiveSchema = Schemas.FirstOrDefault(sch => sch.IsActive);
((PowerSchema)newActiveSchema).IsActive = true;
CurrentSchema = newActiveSchema;
RaisePropertyChangedEvent(nameof(CurrentSchema));
action?.Invoke();
if (oldActiveSchema != null)
{
((PowerSchema)oldActiveSchema).IsActive = false;
}
}
MainWindowViewModel
:
public IPowerSchema ActiveSchema
{
get { return pwrManager.CurrentSchema; }
set
{
if (value != null && !value.IsActive)
{
var action = new Action(() =>
{
CollectionViewSource.GetDefaultView(Schemas).MoveCurrentTo(value);
});
pwrManager.SetPowerSchema(value, action);
}
}
}
, PresentationFramework
. , , , ( PresentationFramework
). (. ) Prism 5.0 MSDN).
# 4
"" . Dispatcher
:
private void setNewCurrSchema(IPowerSchema newActiveSchema)
{
var oldActiveSchema = Schemas.FirstOrDefault(sch => sch.IsActive);
((PowerSchema)newActiveSchema).IsActive = true;
CurrentSchema = newActiveSchema;
RaisePropertyChangedEvent(nameof(CurrentSchema));
if (oldActiveSchema != null)
{
Dispatcher.CurrentDispatcher.InvokeAsync(() =>
{
((PowerSchema)oldActiveSchema).IsActive = false;
});
}
}
WindowsBase
, , , # 3.
# 1 # 2, PowerManager
, # 3 # 4 .