0

INI ファイルから Datagrid の列幅を動的にロードして保存したいと考えています。列幅のサイズ変更ごとに inifile に書き込みます。これにはどのイベントを使用できますか。このための提案やサンプルコードを提供できますか。

4

1 に答える 1

2

私はそのようなことのために動作内でアプリケーション設定を使用し、アプリケーションの終了時に情報を保存します。

利用方法

    <DataGrid>
        <i:Interaction.Behaviors>
            <local:DataGridBehavior GridSettings="{Binding Source={x:Static local:MySettings.Instance},Mode=OneWay}" />
        </i:Interaction.Behaviors>
    </DataGrid>

設定

[SettingsManageabilityAttribute(SettingsManageability.Roaming)]
public sealed class MySettings: ApplicationSettingsBase, IGridSettings
{
    private static readonly Lazy<MySettings> LazyInstance = new Lazy<MySettings>(() => new KadiaSettings());

    public static MySettingsInstance { get { return LazyInstance.Value; } }

    [UserScopedSettingAttribute()]
    [DefaultSettingValue("")]
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    public SerializableDictionary<string, int> GridDisplayIndexList
    {
        get { return (SerializableDictionary<string, int>)this["GridDisplayIndexList"]; }
        set { this["GridDisplayIndexList"] = value; }
    }

    [UserScopedSettingAttribute()]
    [DefaultSettingValue("")]
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    public SerializableDictionary<string, Visibility> GridColumnVisibilityList
    {
        get { return (SerializableDictionary<string, Visibility>)this["GridColumnVisibilityList"]; }
        set { this["GridColumnVisibilityList"] = value; }
    }

    [UserScopedSettingAttribute()]
    [DefaultSettingValue("")]
    [SettingsSerializeAs(SettingsSerializeAs.Xml)]
    public SerializableDictionary<string, double> GridColumnWidthList
    {
        get { return (SerializableDictionary<string, double>)this["GridColumnWidthList"]; }
        set { this["GridColumnWidthList"] = value; }
    }

    private MySettings()
    {
        Application.Current.Exit += OnExit;
    }

    private void OnExit(object sender, ExitEventArgs e)
    {
        this.Save();
    }
}

public interface IGridSettings: INotifyPropertyChanged
{
    SerializableDictionary<string, int> GridDisplayIndexList { get; }

    SerializableDictionary<string, Visibility> GridColumnVisibilityList { get; }

    SerializableDictionary<string, double> GridColumnWidthList { get; }
}

[XmlRoot("Dictionary")]
public class SerializableDictionary<TKey, TValue>: Dictionary<TKey, TValue>, IXmlSerializable 
{

    #region IXmlSerializable Members
    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            var key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            var value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);

            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }
    #endregion
}

行動

public class DataGridBehavior : Behavior<DataGrid>
{

    public static readonly DependencyProperty GridSettingsProperty =
        DependencyProperty.Register("GridSettings", typeof(IGridSettings), typeof(DataGridBehavior), null);


    public IGridSettings GridSettings
    {
        get { return (IGridSettings)GetValue(GridSettingsProperty); }
        set { SetValue(GridSettingsProperty, value); }
    }

    public DataGridICollectionViewSortMerkerBehavior()
    {
        Application.Current.Exit += CurrentExit;
    }

    private void CurrentExit(object sender, ExitEventArgs e)
    {
        SetSettings();
    }

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.Loaded += AssociatedObjectLoaded;
        AssociatedObject.Unloaded += AssociatedObjectUnloaded;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.Loaded -= AssociatedObjectLoaded;
        AssociatedObject.Unloaded -= AssociatedObjectUnloaded;
    }

    private void AssociatedObjectUnloaded(object sender, RoutedEventArgs e)
    {
        SetSettings();
    }

    void AssociatedObjectLoaded(object sender, RoutedEventArgs e)
    {
        var settings = GridSettings;

        var columns = AssociatedObject.Columns.ToList();
        var colCount = columns.Count;
        foreach (var column in columns)
        {
            var key = column.Header.ToString();

            if (settings.GridDisplayIndexList.ContainsKey(key))
            {
                //manchmal wird -1 als index abgespeichert
                var index = settings.GridDisplayIndexList[key];
                if(index > 0 && index < colCount)
                    column.DisplayIndex = index;
            }

            if (settings.GridColumnVisibilityList.ContainsKey(key))
            {
                column.Visibility = settings.GridColumnVisibilityList[key];
            }

            if (settings.GridColumnWidthList.ContainsKey(key))
            {
                column.Width = new DataGridLength(settings.GridColumnWidthList[key]);
            }
        }           
    }


    private void SetSettings()
    {
        var settings = GridSettings;

        foreach (var column in AssociatedObject.Columns)
        {
            var key = column.Header.ToString();
            var displayindex = column.DisplayIndex;
            var visibility = column.Visibility;
            var width = column.ActualWidth;

            if (settings.GridDisplayIndexList.ContainsKey(key))
            {
                settings.GridDisplayIndexList[key] = displayindex;
            }
            else
            {
                settings.GridDisplayIndexList.Add(key, displayindex);
            }

            if (settings.GridColumnVisibilityList.ContainsKey(key))
            {
                settings.GridColumnVisibilityList[key] = visibility;
            }
            else
            {
                settings.GridColumnVisibilityList.Add(key, visibility);
            }

            if (settings.GridColumnWidthList.ContainsKey(key))
            {
                settings.GridColumnWidthList[key] = width;
            }
            else
            {
                settings.GridColumnWidthList.Add(key, width);
            }
        }
    }

}
于 2013-10-28T06:32:42.660 に答える