2

ここでは、基本的に 1 つのペインにフォルダー ツリーを表示し、別のペイン (リストビュー) にそのディレクトリ内のファイルを表示する WPF のユーザー コントロールを 1 つ持っています。

ここで、基本的に特定のファイルをリストビューでのみ表示するために必要な、fileextensionfilter と呼ばれる 1 つのプロパティを公開しました。たとえば、fileextensionfilter= XML の場合、xml ファイルのみが表示されます。

現在、私のメインアプリケーションでは、上記のコントロールを 3 回使用していますが、異なるファイル拡張子ファイルを使用しています (例: 1>xml 別のインスタンス .pdf のみなど)。

今、settings.default.xmlfilter、settings.default.PDFFilterなどから拡張フィルター値を取得しています......

ここでの問題は、usercontrol のコントロール プロパティをロードするときに初期化されず、このプロパティを使用するコンストラクターに何かがあるため (その時点で "null")、フィルターが最初に機能しないことです。次にもう一度更新すると、フィルターのプロパティが適用されるため、機能します。

4

1 に答える 1

1

現在のプロパティを使用してみて、Loadedイベントを使用して、現在コンストラクターで実行しているコードを実行できます。以下に小さな例を示します。

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
    <Grid>
        <my:UserControl1 FileExtensionFilter="RTF" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="userControl1" VerticalAlignment="Top" />
        <my:UserControl1 FileExtensionFilter="XML" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="userControl2" VerticalAlignment="Top" />
        <my:UserControl1 FileExtensionFilter="PDF" HorizontalAlignment="Left" Margin="10,10,0,0" x:Name="userControl3" VerticalAlignment="Top" />
    </Grid>
</Window>

ユーザーコントロール

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        string filter = "NULL";
        public UserControl1()
        {
            InitializeComponent();
            System.Diagnostics.Debug.WriteLine("Property" + filter + "Set during Constructor");
        }

        public string FileExtensionFilter
        {
            get { return filter; }
            set { filter = value; }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Property" + filter + "Set during Loaded");
        }

        private void UserControl_Initialized(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Property" + filter + "Set during Initialized");
        }
    }
}

出力

初期化
中の PropertyNULLSet コンストラクタ
中の PropertyNULLSet PropertyNULLSet 初期化
中の PropertyNULLSet コンストラクタ中の
PropertyNULLSet 初期化中の PropertyNULLSet
コンストラクタ
中の
PropertyNULLSet

于 2012-11-23T04:13:32.743 に答える