Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6697559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:28:26+00:00 2026-05-26T06:28:26+00:00

I have a Silverlight project I’m converting to MVVM. It’s my first time using

  • 0

I have a Silverlight project I’m converting to MVVM. It’s my first time using the pattern and I’m struggling with something.

So basically I had this in the XAML code behind page:

 OpenFileDialog ofd = new OpenFileDialog();
            if ((bool)ofd.ShowDialog())
            {
                _fileName = ofd.File.Name;
                FileStream fs = ofd.File.OpenRead();
                fileSize = (double)fs.Length;
                txtFileName.Text = fileName;
                index = 0;
                sendData = 0;

                byte[] file = new byte[fs.Length];
                fs.Read(file, 0, file.Length);
                //convertToChunks(file);

                prgUpload.Maximum = fileChunks.Count;
                prgUpload.Value = 0;
                //uploadChunks(index);
            }

And I cannot figure out how to wire it up to be able to use that in the model? I assume the viewmodel comes into play, but nothing is working.

Any thoughts?

Here is the work in progress XAML:

<Grid x:Name="LayoutRoot" Width="475" Height="340">
    <Canvas Margin="8,8,0,0" Background="White" Height="320" VerticalAlignment="Top" HorizontalAlignment="Left" Width="475">
        <Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse" />
        <TextBox Canvas.Left="25" IsReadOnly="True"  Canvas.Top="43" TextWrapping="Wrap" Width="350" Text="{Binding Path=FileUploadName}" x:Name="txtFileName" />
        <ProgressBar Height="10" Width="350" Canvas.Left="25" Canvas.Top="99" x:Name="prgUpload" />

        <my:Label Content="Please select a file to upload"  Name="lblError" Canvas.Left="25" Canvas.Top="23" RenderTransformOrigin="0.133,-0.063" Width="220"/>
        <my:Label x:Name="lblProgress" Canvas.Left="25" Canvas.Top="78" RenderTransformOrigin="0.133,-0.063" Width="220"/>
    </Canvas>
</Grid>

Basically I want it to fire after the user selects a file to upload.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:28:27+00:00Added an answer on May 26, 2026 at 6:28 am

    If you want to fire a command this would do the work for you

    <Button Width="75" Canvas.Left="380" Canvas.Top="43" Content="Browse" x:Name="btnBrowse" 
      Command={Binding OpenFileCommand} />
    

    in your code behind Constructor for example

    partial class MainWindow
    {
       public MainWindow()
       {
         InitializeComponent();
         this.DataContext=new MainViewModel();
       }
    }
    

    and in your ViewModel

       public ICommand OpenFileCommand { get; set; }
    
       public MainViewModel()
       {
           OpenFileCommand = new RelayCommand(OpenDialog) { IsEnabled = true };
    
       }
    
       private void OpenDialog()
       {
           OpenFileDialog ofd = new OpenFileDialog();
           if ((bool)ofd.ShowDialog())
           {
               _fileName = ofd.File.Name;
               FileStream fs = ofd.File.OpenRead();
               fileSize = (double)fs.Length;
               //txtFileName.Text = fileName;// Apply Binding 
               index = 0;
               sendData = 0;
    
               byte[] file = new byte[fs.Length];
               fs.Read(file, 0, file.Length);
               //convertToChunks(file);
    
               prgUpload.Maximum = fileChunks.Count;
               prgUpload.Value = 0;
               //uploadChunks(index);
           }
       }
    

    And the RelayCommand

    public class RelayCommand:ICommand
    {
       private bool _isEnabled;
       public bool IsEnabled
       {
           get { return _isEnabled; }
           set
           {
               if (value != _isEnabled)
               {
                   _isEnabled = value;
                   if (CanExecuteChanged != null)
                   {
                       CanExecuteChanged(this, EventArgs.Empty);
                   }
               }
           }
       }
       private Action _handler;
       public RelayCommand(Action handler)
       {
           _handler = handler;
       }
    
    
       public bool CanExecute(object parameter)
       {
           return IsEnabled;
       }
    
       public event EventHandler CanExecuteChanged;
    
       public void Execute(object parameter)
       {
           _handler();
       }
    }
    

    in order to get the filename in your textbox you have to bind the textbox to to the view model. so that it will appear on the UI and also implement INotifyPropertyChanged. Also Look at this it would be helpful Silverlight MVVM

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Silverlight 3 project with something like this: <ItemGroup> <Content Include=Content\image1.png> </Content>
I have a Silverlight Project which passes data using the Data Contract to a
I have created a web application with a Silverlight project embedded in it, using
This might be a bit tricky setup but... I have this Silverlight project that
I have a Silverlight testing project using the Silverlight Unit Test Framework . I
I have Silverlight Sketch project, and I`m using muliticolumn listview to simulate datagrid. <Style
First, I have two project working on: ASP.NET and Silverlight Both uses a class
I have a common silverlight project. This project project, among other things, includes constants
I have created a Silverlight Application project using the Bing Maps Silverlight Control and
I have a Silverlight project (with ASP.net MVC web project) Suddenly, when I press

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.