简体   繁体   中英

Expose property for WPF user control

Here i've one user- control in WPF, which basically a shows folder tree in one pane and another pane(listview) shows files in that directory.

Now i exposed one property called fileextensionfilter which basically required to shows specific file only in listview. eg if fileextensionfilter= XML it shows only xml files.

Now in my main application i am using the above control thrice, but with differen fileextensionfilet eg 1>xml another instance .pdf only and so on....

now i am getting the extension filter value from settings.default.xmlfilter,settings.default.PDFFilter so on......

here problem is when i load control properties of usercontrol is not initialized and i have something in constructor which makes uses of this properties and (at that time "null") so filter not working on first time. next once again its get refreshed the filter porperty will get applied and hence it works.

You can try using your current property and use the Loaded Event to run the Code you are currently running in your constructor. Here is a small example:

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>

UserControl

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");
        }
    }
}

Output

PropertyNULLSet during Initialized
PropertyNULLSet during Constructor
PropertyNULLSet during Initialized
PropertyNULLSet during Constructor
PropertyNULLSet during Initialized
PropertyNULLSet during Constructor
PropertyRTFSet during Loaded
PropertyXMLSet during Loaded
PropertyPDFSet during Loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM