简体   繁体   中英

xaml binding not working as expected

I have created a simple credo (create, review, edit, delete, overview) wpf application for cars to learn about c# and have run into an issue. When either adding or editing an item to my observable collection, I want to allow the user to be able to browse the computer for a picture associated with the car. I originally had accomplished this in code behind with the following:

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

This worked perfectly but then I was requested to remove all code behind (which seemed fine to be because this is purely a UI element, tell me if I'm wrong) from the application. So I moved the code into an 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");

        }

    }
}
}

Now when I run the application, the path does not show up in the textbox, when when I click the "add to list" button, the item is added displaying the proper image. It seems as though the textbox is not updating, even though I have INotification implemented. Am I missing something obvious? Here is relevant the xaml code:

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

and

<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" />

If I understand this correctly, you are binding to NewCar.PicturePath but only NewCar is changing. When you Notify of property changed, I am guessing you only notify that the NewCar object is changing (not the PicturePath). Try this; within the NewCar setter, after setting _NewCar = value, also update _NewCar.PicturePath and assuming PicturePath also is notifying properly, it should update properly and also give you some insight as to how the binding is working... Hope this helps!

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