みなさん、こんにちは。
エキスパンダーの項目を変更した後、エキスパンダー ヘッダーの前景色を変更するのに問題があります。
ここに私がすでに持っている私のWPFスニペットがあります:
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Margin="10,0,0,0" IsExpanded="True"
BorderBrush="{Binding ., Converter={StaticResource BackgroundHeader}}">
<Expander.Background>
<Binding Path="." Converter="{StaticResource BackgroundHeader}" />
</Expander.Background>
<Expander.Header >
<DockPanel Background="{Binding .,Converter={StaticResource BackgroundHeader}}">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding ., Converter={StaticResource ForegroundHeader}}"
Margin="5,0,0,0"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
値を変更した後に ListCollectionView を更新しようとしましたが、パフォーマンスが非常に悪いです。
MyListCollectionView.Refresh();
助けてくれてありがとう...
更新:GroupStyle
これが定義されている
内の私のWPFスニペットです:
<DataGrid x:Name="DataGridItemList" Grid.Row="1" ItemsSource="{Binding Path= CurrentWorkItemList}"
SelectedItem="{Binding Path= CurrentSelectedItem}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<.../>
</DataGridTemplateColumn>
<DataGridTemplateColumn x:Name="SelectedCol" Header=" " CanUserResize="False">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<CheckBox x:Name="ckbIsSelected" Click="CkbIsSelected_OnClick" IsChecked="{Binding Path = IsSelected, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Center"
IsEnabled="{Binding Path = Mandatory, Converter={StaticResource InvertBoolean}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<.../>
</DataGridTemplateColumn>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Margin" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander Margin="10,0,0,0" IsExpanded="True"
BorderBrush="{Binding ., Converter={StaticResource BackgroundHeader}}">
<Expander.Background>
<Binding Path="." Converter="{StaticResource BackgroundHeader}" />
</Expander.Background>
<Expander.Header >
<DockPanel Background="{Binding .,Converter={StaticResource BackgroundHeader}}">
<TextBlock FontWeight="Bold" Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
Foreground="{Binding ., Converter={StaticResource ForegroundHeader}}"
Margin="5,0,0,0"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter />
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Foreground" Value="Black" />
<Setter Property="Background" Value="White" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
...そしてこのViewのViewModel:
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
...
}
#region Properties
private ListCollectionView _currentWorkItemList;
public ListCollectionView CurrentWorkItemList
{
get { return _currentWorkItemList; }
set
{
_currentWorkItemList = value;
OnPropertyChanged("CurrentWorkItemList");
}
}
private MyClass _CurrentSelectedItem;
public MyClass CurrentSelectedItem
{
get { return _CurrentSelectedItem; }
set
{
var saved = CheckChangesAndSaveItem(_CurrentSelectedItem);
_CurrentSelectedItem = value;
if (_CurrentSelectedItem != null)
{
SetCurrentSelectedItemToCache(_CurrentSelectedItem);
}
OnPropertyChanged("CurrentSelectedItem");
}
}
private MyClass _selectedItemCache;
public MyClass SelectedItemCache
{
get { return _selectedItemCache; }
set
{
_selectedItemCache = value;
OnPropertyChanged("SelectedItemCache");
}
}
#endregion
#region Public Functions/Commands
public void InitViewModel()
{
using (new WaitCursor())
{
CurrentWorkItemList = new ListCollectionView(...);
if (CurrentWorkItemList.GroupDescriptions != null)
{
CurrentWorkItemList.GroupDescriptions.Add(new PropertyGroupDescription("Group1"));
CurrentWorkItemList.GroupDescriptions.Add(new PropertyGroupDescription("Group2"));
}
}
}
public void CheckSaveAndCloneCurrentItem()
{
CheckSaveAndCloneCurrentItem(_CurrentSelectedItem);
}
#endregion
#region Internal Functions
private void CheckSaveAndCloneCurrentItem(MyClass currentItem)
{
var saved = CheckChangesAndSaveItem(currentItem);
SetCurrentSelectedItemToCache(currentItem);
}
private void SetCurrentSelectedItemToCache(MyClass currentItem)
{
if (currentItem != null)
{
SelectedItemCache = currentItem.Clone();
}
}
private bool CheckChangesAndSaveItem(MyClass currentItem)
{
var eItemIsSelected = ViewModels.DataAdapter.GetWorkItem().IsSelected;
if (currentItem.IsSelected != SelectedItemCache.IsSelected || SelectedItemCache == null ||
(currentItem.IsSelected != eItemIsSelected))
{
currentItem.Group1 = currentItem.Group1.ToString();
ViewModels.DataAdapter.SaveAllListItem(currentItem);
return true;
}
return false;
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}