使用TriggerAction显示SaveFileDialog

时间:2014-09-06 11:10:29

标签: c# wpf prism filedialog

我正在尝试从我的应用程序中的SaveFileDialog获取文件名。

<Button Content="Raise" Click="Click" Name="btn">
    <i:Interaction.Triggers>
        <prism:InteractionRequestTrigger SourceObject="{Binding SaveFileRequest, Mode=OneWay}">
            <library:ShowSaveDialogAction/>
        </prism:InteractionRequestTrigger>
    </i:Interaction.Triggers>
</Button>

这是我的ShowSaveDialogAction

public class ShowSaveDialogAction : TriggerAction<FrameworkElement>
{
    public string FileName
    {
        get { return (string)GetValue(FileNameProperty); }
        set { SetValue(FileNameProperty, value); }
    }

    public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ShowSaveDialogAction), new PropertyMetadata(""));

    protected override void Invoke(object o)
    {
        SaveFileDialog sfd = new SaveFileDialog();
        if (sfd.ShowDialog() == true)
        {
            FileName = sfd.FileName;

        }
    }
}

这是我的XAML背后的代码:

public partial class Shell : Window
{
    public Shell()
    {
        InitializeComponent();
        SaveFileRequest = new InteractionRequest<ISaveConfirmation>();
        this.DataContext = this;

    }

    public InteractionRequest<ISaveConfirmation> SaveFileRequest { get; set; }

    private void Click( object sender, RoutedEventArgs e )
    {
        SaveFileRequest.Raise( new SaveConfirmation { Title = "" }, OnAnswer );
    }

    private void OnAnswer( ISaveConfirmation obj )
    {
        if (obj.Confirmed)
        {
            btn.Content = obj.FileName;
        }
    }
}

所以我不知道接下来该做什么,以及如何在按钮上的对话框中显示文件名。我如何将所有东西连接在一起?

0 个答案:

没有答案