xaml绑定未按预期工作

时间:2015-06-18 19:09:44

标签: c# wpf xaml

我为汽车创建了一个简单的信条(创建,审查,编辑,删除,概述)wpf应用程序,以了解c#并遇到问题。在我的可观察集合中添加或编辑项目时,我想让用户能够浏览计算机以查找与汽车相关的图片。我最初在代码中用以下内容完成了这个:

namespace CarApp
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    /// <summary>
    /// Open a browser window when the user clicks on the 'browse' button
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Add_Browse_Button_Click(object sender, RoutedEventArgs e)
    {
        //send path to textbox
        AddPicturePath.Text = this.Browse();
    }

    ///// <summary>
    ///// Open a browser window when the user clicks on the 'browse' button
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="e"></param>
    private void Edit_Browse_Button_Click(object sender, RoutedEventArgs e)
    {
        //send path to textbox
        EditPicturePath.Text = this.Browse();
    }

    /// <summary>
    /// Open browser window and return user selection
    /// </summary>
    /// <returns>filename</returns>
    private string Browse()
    {
        //open browser window
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        //search only for .png files
        dlg.DefaultExt = ".png";

        //show browser
        dlg.ShowDialog();

        return dlg.FileName;
    }
}
}

这完美地工作但后来我被要求从应用程序中删除所有代码(这看起来很好,因为这纯粹是一个UI元素,告诉我,如果我错了)。所以我把代码移到了ICommand:

namespace CarApp.Commands
{
public class BrowseCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        Car param = parameter as Car;

        try
        {
            //open browser window
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

            //search only for .png files
            dlg.DefaultExt = ".png";

            //show browser
            dlg.ShowDialog();

            //send path to textbox
            param.PicturePath = dlg.FileName;
        }
        catch (NullReferenceException)
        {
            Console.Write("Param is null in browse");

        }

    }
}
}

现在,当我运行应用程序时,路径不会显示在文本框中,当我点击&#34;添加到列表&#34;按钮,添加项目显示正确的图像。好像文本框没有更新,即使我已经实现了INotification。我错过了一些明显的东西吗这与xaml代码相关:

<Button Width="75"
    Height="23"
    Margin="10,0,0,0"
    HorizontalAlignment="Left"
    Command="{Binding BrowseCommand}"
    CommandParameter="{Binding NewCar}"
    Content="Browse" />

<TextBox x:Name="AddPicturePath"
        Width="200"
        Height="23"
        Margin="10,0,0,0"
        HorizontalAlignment="Left"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        Text="{Binding NewCar.PicturePath,
                    UpdateSourceTrigger=PropertyChanged}"
        TextWrapping="Wrap" />

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你绑定到NewCar.PicturePath但只有NewCar正在改变。当您通知属性发生更改时,我猜您只通知NewCar对象正在更改(而不是PicturePath)。试试这个;在NewCar setter中,设置_NewCar = value之后,还要更新_NewCar.PicturePath并假设PicturePath也正常通知,它应该正确更新,并且还能让你深入了解绑定是如何工作的......希望这会有所帮助!