Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置C#xaml进度条';将最大值转换为变量值_C#_Xaml_Variables_Max - Fatal编程技术网

设置C#xaml进度条';将最大值转换为变量值

设置C#xaml进度条';将最大值转换为变量值,c#,xaml,variables,max,C#,Xaml,Variables,Max,我的代码中有一个xaml进度条,我希望将最大值设置为代码中其他地方确定的整数。我该怎么做 <ProgressBar Name="pb_Run" HorizontalAlignment="Left" Height="4" Margin="0,166,0,0" VerticalAlignment="Top" Width="960" IsIndeterminate="False" Minimum="0" Maximum="**some_variable_value_here**"/>

我的代码中有一个xaml进度条,我希望将最大值设置为代码中其他地方确定的整数。我该怎么做

<ProgressBar Name="pb_Run" HorizontalAlignment="Left" Height="4" Margin="0,166,0,0"     VerticalAlignment="Top" Width="960" IsIndeterminate="False" Minimum="0" Maximum="**some_variable_value_here**"/>


一些补充资料。变量值在用户的xaml界面选择文件后确定。它的值等于文件中行数的计数。每次更新时,进度条都会增加一个固定的整数值。

您需要执行以下操作:

if(_needToChangeProgressBarValue)
{
    pb_Run.Maximum = yourvariable;
}

您需要这样做:

if(_needToChangeProgressBarValue)
{
    pb_Run.Maximum = yourvariable;
}

您可以使用WPF的data Binding将进度条的最大值与代码隐藏中的值绑定


查看示例。

您可以使用WPF的数据绑定将进度条的最大值与代码中的值绑定


检查示例。

进度条问题在该问题之后出现。我不需要使用xaml代码。最终设置最大值非常简单,只需添加以下代码:

pb_Run.Maximum = loanCNT;
更大的问题是如何增加进度条的价值。事实证明,将进度条值增加到for循环(紧密循环)需要调度程序。为此,添加了以下代码:

      UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(pb_Run.SetValue);
        pb_Run.Maximum = loanCNT;
        double progressBarValue=0;

    private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);
以及新的for循环:

for (int obs = 0; obs < loanCNT; obs++)
                {
                    line = sr.ReadLine();
                    Processing.Run(new Loan(), line);
                    progressBarValue += 1;
                    Dispatcher.Invoke(updatePbDelegate,System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, progressBarValue });
                }
for(int-obs=0;obs

如果有人有类似的问题,希望这能对他们有所帮助。

在这个问题之后,进度条问题变得越来越严重。我不需要使用xaml代码。最终设置最大值非常简单,只需添加以下代码:

pb_Run.Maximum = loanCNT;
更大的问题是如何增加进度条的价值。事实证明,将进度条值增加到for循环(紧密循环)需要调度程序。为此,添加了以下代码:

      UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(pb_Run.SetValue);
        pb_Run.Maximum = loanCNT;
        double progressBarValue=0;

    private delegate void UpdateProgressBarDelegate(System.Windows.DependencyProperty dp, Object value);
以及新的for循环:

for (int obs = 0; obs < loanCNT; obs++)
                {
                    line = sr.ReadLine();
                    Processing.Run(new Loan(), line);
                    progressBarValue += 1;
                    Dispatcher.Invoke(updatePbDelegate,System.Windows.Threading.DispatcherPriority.Background, new object[] { ProgressBar.ValueProperty, progressBarValue });
                }
for(int-obs=0;obs
如果有人有类似的问题,希望这能帮助他们