1

On MainWindow.xaml.cs I defined:

private int _currentStep = 1;
public int CurrentStep
{
    get
    {
        return _currentStep;
    }

    set
    {
        _currentStep = value;
        NotifyPropertyChanged("CurrentStep");
    }
}

private void NotifyPropertyChanged(string info)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
}
public event PropertyChangedEventHandler PropertyChanged;

And I'm also update CurrentStep in same file.

On MessageWithProgressBar.xaml I defined:

<Window x:Class="Db.MessageWithProgressBar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Db"
    mc:Ignorable="d"
    Title="DB Processing" Height="150" Width="300" Background="{DynamicResource {x:Static SystemColors.GradientInactiveCaptionBrushKey}}">
<Window.Resources>
    <local:currentStepToProgressValue x:Key="stepToProgress"/>
</Window.Resources>
<Grid DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}">
    <Grid.RowDefinitions>
        <RowDefinition Height="60"/>
        <RowDefinition Height="60"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Label Name="title" Content="Please wait while processing.." FontSize="17.333"/>
    </Grid>
    <Grid Grid.Row="1" Margin="0,0,0,20">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}" />
    </Grid>
</Grid>

The error I got on runtime is:

BindingExpression path error: 'CurrentStep' property not found on 'object' ''String' (HashCode=1137317725)'. BindingExpression:Path=CurrentStep; DataItem='String' (HashCode=1137317725); target element is 'ProgressBar' (Name='progress'); target property is 'Value' (type 'Double')

Any assistance?

Thanks!

slugster
  • 49,403
  • 14
  • 95
  • 145
Programmer
  • 371
  • 1
  • 8
  • 21
  • Why are you trying to link the progress bar with a property in another window? Why not just inject a value when you show `MessageWithProgressBar`? If the value cannot be injected into MessageWithProgressBar, then consider injecting the work as a delegate instead so that it can be captured and measured in MessageWithProgressBar. – slugster Jun 05 '16 at 12:14
  • @slugster In MainWindow.xaml.cs I processing the job and while processing the job I want to raise another window (which display progressbar) as a thread and the progress value will be update according job step which set as wrote above in MainWindow.xaml.cs – Programmer Jun 05 '16 at 12:18
  • I guessed what you are trying to do, and you are going about it the wrong way.... you need a dialog service, there are plenty of examples out there with varying degrees of sophistication. Here are two previous SO questions to get you started: [*Good or bad practice for Dialogs in wpf with MVVM?*](http://stackoverflow.com/q/3801681/109702), [*Standard Approach of Launching Dialogs/Child Windows from a WPF Application using MVVM*](http://stackoverflow.com/q/17308945/109702) – slugster Jun 05 '16 at 12:23

1 Answers1

1

You have to set the DataContext of the MessageBox. You could pass the MainWindow in constuctor during creation:

MessageWithProgressBar.xaml.cs

public MessageWithProgressBar(MainWindow Mw)
{
  this.DataContext = Mw;
}

Now you can bind every property only with it's name. Your progressBar looks now like

<ProgressBar Name="progress" IsIndeterminate="True" Value="{Binding CurrentStep, Converter={StaticResource stepToProgress}}"/>
Fruchtzwerg
  • 10,999
  • 12
  • 40
  • 49