具有M-V-VM模式的WPF的OpenFileDialog

时间:2011-08-18 15:11:22

标签: wpf mvvm openfiledialog

我需要使用OpenFileDialog来选择一个文件,但我不能使用像Galgasoft这样的任何MVVM中心工具包,它允许我这样做而不违反MVVM模式。

我还能怎样做到这一点?

2 个答案:

答案 0 :(得分:0)

当然,这是我用来读取Excel文件的一些代码的示例。它位于ViewModel并从SelectFileCommand

调用
private void SelectFile()
{
    var dlg = new OpenFileDialog();
    dlg.DefaultExt = ".xls|.xlsx";
    dlg.Filter = "Excel documents (*.xls, *.xlsx)|*.xls;*.xlsx";

    if (dlg.ShowDialog() == true)
    {
        var file = new FileInfo(dlg.FileName);
        ReadExcelFile(file.FullName);
    }
}

private void ReadExcelFile(fileName)
{
    try
    {
        using (var conn = new OleDbConnection(string.Format(@"Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", fileName)))
        {
            OleDbDataAdapter da = new OleDbDataAdapter("SELECT DISTINCT [File Number] FROM [Sheet1$]", conn);
            var dt = new DataTable();
            da.Fill(dt);

            int i;
            FileContents = (from row in dt.AsEnumerable()
                            where int.TryParse(row[0].ToString(), out i)
                            select row[0]).ToList()
                            .ConvertAll<int>(p => int.Parse(p.ToString()));
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Unable to read contents:\n\n" + ex.Message, "Error");
    }
}

您需要为Microsoft.Win32

引用OpenFileDialog

答案 1 :(得分:0)

您可以创建自定义控件,这样您就可以将其中的字符串绑定到viewmodel属性。

我通常创建的自定义控件由以下内容组成:

  • 文本框或文本块
  • 以图像为模板的按钮
  • 将文件路径包装到
  • 的字符串依赖项属性

所以* .xaml文件就像这样

<Grid>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

    <TextBox Grid.Column="0" Text="{Binding Text, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
    <Button Grid.Column="1"
            Click="Button_Click">
        <Button.Template>
            <ControlTemplate>
                <Image Grid.Column="1" Source="../Images/carpeta.png"/>
            </ControlTemplate>                
        </Button.Template>
    </Button>

</Grid>

和* .cs文件:

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text",
        typeof(string),
        typeof(customFilePicker),
        new FrameworkPropertyMetadata(
            null,
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault | FrameworkPropertyMetadataOptions.Journal));

    public string Text
    {
        get
        {
            return this.GetValue(TextProperty) as String;
        }
        set
        {
            this.SetValue(TextProperty, value);
        }
    }

    public FilePicker()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();

        if(openFileDialog.ShowDialog() == true)
        {
            this.Text = openFileDialog.FileName;
        }
    }

最后,您可以将其绑定到您的视图模型:

<controls:customFilePicker Text="{Binding Text}"}/>