首页 > 解决方案 > 数据绑定进度条未绑定

问题描述

我为此搜索了很多解决方案,但似乎无法弄清楚。(只是说,这将是一个很长的帖子)

前言:这不是 MVVM

问题:我有一个进度条。我目前正在尝试从 xaml 的 .cs 文件中将公共双精度绑定到它。但是,每当我使用 value 的 bind 函数时,它永远不会起作用,导致 iNotifyPropertyChanged 不起作用,等等......

它绑定的变量用于更新进度条值。完成的方法是在另一个方法/类的单独 for 循环中设置值。该值由一个简单的方程式设置。100 除以列表的长度,再乘以 for 循环计数器。这给了我们一个可以放入进度条值的实数。(下面的代码)

    Binder.daWindow.progUpdate = ((100.00 / databaseTables.Length) * dbTIndex);

我知道这可以通过线程(或其他方式)来完成,但我会特别感谢有关如何通过绑定来完成的答案(除非不可能,然后忘记我刚才所说的)。

XAML:

<Window x:Name="DateWindow1" x:Class="WpfApp1.DateWindow"
    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:WpfApp1"
    xmlns:Main="using:DateWindow"
    mc:Ignorable="d"
    Title="Change Dates" Height="232.667" Width="462" Background="#FF2B2B2B" ResizeMode="NoResize">



    <Grid>
    <TextBox x:Name="DayChangeTextBox" HorizontalAlignment="Left" Height="30" Margin="176,45,0,0" TextWrapping="Wrap" Text="##" VerticalAlignment="Top" Width="58" FontSize="14" TextAlignment="Center" AcceptsReturn="False" AcceptsTab="False" GotFocus="DayChangeTextBox_GotFocus" LostFocus="DayChangeTextBox_LostFocus" BorderThickness="1" BorderBrush="Black" OpacityMask="Black" Background="#FFFDFDFD"/>
    <Label Content="Days" HorizontalAlignment="Left" Margin="239,45,0,0" VerticalAlignment="Top" Width="42" FontSize="14" FontFamily="Microsoft YaHei UI" Height="30" Foreground="White"/>
    <Label Content="Move Dates Forward:" HorizontalAlignment="Left" Margin="23,45,0,0" VerticalAlignment="Top" Width="144" FontSize="14" FontFamily="Microsoft JhengHei UI Light" Height="30" Foreground="White"/>
    <Button x:Name="ChangeDatesButton" Content="Change Dates" HorizontalAlignment="Left" Margin="303,45,0,0" VerticalAlignment="Top" Width="143" Height="30" BorderThickness="1,1,2,3" BorderBrush="#FF474747" Click="ChangeDatesButton_Click" FontFamily="Microsoft YaHei UI" Background="#FFE6E6E6"/>
    <Label x:Name="serverLabel" Content="Label" HorizontalAlignment="Left" Margin="10,73,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="5" Height="15" Visibility="Hidden"/>
    <Label x:Name="databaseLabel" Content="Label" HorizontalAlignment="Left" Margin="20,73,0,0" VerticalAlignment="Top" Width="3" Visibility="Hidden" Height="15"/>
    <Button x:Name="RevertToDefaultButton" Content="Revert to Backup" Margin="23,164,0,0" VerticalAlignment="Top" Height="30" Background="#FFE6E6E6" BorderBrush="#FF474747" BorderThickness="1,1,2,3" Click="RevertToDefaultButton_Click" FontFamily="Microsoft JhengHei UI" HorizontalAlignment="Left" Width="143"/>
    <Button x:Name="OverwriteDefaultButton" Content="Backup Database" Margin="23,128,0,0" VerticalAlignment="Top" Height="30" BorderBrush="#FF474747" BorderThickness="1,1,2,3" Click="OverwriteDefaultButton_Click" FontFamily="Microsoft JhengHei UI" HorizontalAlignment="Left" Width="143" Background="#FFE6E6E6" />
    <Button x:Name="CloseButton" Content="Close" Margin="0,164,10,0" VerticalAlignment="Top" Height="30" BorderBrush="#FF474747" BorderThickness="1,1,2,3" FontFamily="Microsoft JhengHei UI" Width="60" Click="CloseButton_Click" HorizontalAlignment="Right" Background="#FFE6E6E6" />
    <Label x:Name="RevertBackupLabel" Content="Backup or Revert Database:" HorizontalAlignment="Left" Margin="12,93,0,0" VerticalAlignment="Top" Foreground="White" FontFamily="Microsoft JhengHei UI" FontWeight="Bold" FontSize="16"/>
    <Label x:Name="DatesLabel" Content="Change Dates in Database:" Margin="12,10,0,0" Foreground="White" FontFamily="Microsoft JhengHei UI" FontWeight="Bold" FontSize="16" HorizontalAlignment="Left" Width="216" Height="30" VerticalAlignment="Top"/>
    <ProgressBar HorizontalAlignment="Left" Height="13" Margin="303,80,0,0" VerticalAlignment="Top" Width="143" Name="pbStatus"  Value="{Binding local.progUpdate, UpdateSourceTrigger=PropertyChanged}" Maximum="{Binding mx}" Minimum="{Binding mn}" LargeChange="0.01"/>
</Grid>

XAML CS 部分:

public static class Binder
{
    public static DateWindow daWindow;
}
public partial class DateWindow : Window, INotifyPropertyChanged
{

    private double _progUpdate;
    public double progUpdate
    {
        get { return _progUpdate; }
        set
        {
            _progUpdate = value;
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(progUpdate)));
            }

        }

    }
    public double mx = 100;
    public double mn = 0; 


    public event PropertyChangedEventHandler PropertyChanged;
    public DateWindow()
    {
        InitializeComponent();
        Binder.daWindow = this; 
        ChangeDateInDatabase ChangeDate = new ChangeDateInDatabase();
        progUpdate = 25; // This is there so we can test the variable without going through the whole program. 
    }

    private void ChangeDatesButton_Click(object sender, RoutedEventArgs e)
    {
        ChangeDateInDatabase ChangeDate = new ChangeDateInDatabase();
        var result = MessageBox.Show("Are you sure you would like to change dates for  " + Globals.selectedDatabase + " from server " + Globals.main.ServerComboBox.Text + "?", "Change Dates?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
        if (Globals.main.ServerComboBox.Text != "" && Globals.selectedDatabase != "" && FindDatabaseAndServer.Connector(Globals.main.ServerComboBox.Text, Globals.selectedDatabase) && result == MessageBoxResult.Yes)
        {
            try
            {
                long.Parse(DayChangeTextBox.Text);
                ChangeDate.ChangeDates(this); 
                //ChangeDateInDatabase.ChangeDates(this);

            }
            catch
            {
                MessageBox.Show($"Invalid number {DayChangeTextBox.Text}, please enter a valid number.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

(CLICK 函数用于调用程序的方法。在方法内部,它的主循环确定方法的完成,包含希望与定义方程绑定的变量以获取其值)

包含所述循环的方法:

for (int dbTIndex = 0; dbTIndex < databaseTables.Length; dbTIndex++)
                    {
                        string[] dateColumns = new string[0];
                        string columnSelectQuery = "SELECT DATA_TYPE, COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS  WHERE TABLE_NAME = '" + databaseTables[dbTIndex].ToString() + "' AND COLUMN_NAME LIKE '_%'";
                        using (SqlConnection connection2 = new SqlConnection(connectionString))
                        {
                            SqlCommand command2 = new SqlCommand(columnSelectQuery, connection2);
                            connection2.Open();
                            SqlDataReader reader2 = command2.ExecuteReader();
                                int y = 0;
                                while (reader2.Read())
                                {
                                    if (reader2[0].ToString() == "datetime")
                                    {
                                        Array.Resize(ref dateColumns, dateColumns.Length + 1);
                                        dateColumns[y] = reader2[1].ToString();
                                        y++;
                                    }
                                }
                        }
                        for (var dtColIndex = 0; dtColIndex < dateColumns.Length; dtColIndex++)
                        {
                            string addDatesQuery = "UPDATE " + databaseTables[dbTIndex] + " SET " + dateColumns[dtColIndex] + " = DATEADD(dd," + Convert.ToInt32(date.DayChangeTextBox.Text) + "," + dateColumns[dtColIndex] + ") WHERE " + dateColumns[dtColIndex] + " IS NOT NULL";

                            using (SqlConnection connection3 = new SqlConnection(connectionString))
                            {
                                    SqlCommand command3 = new SqlCommand(addDatesQuery, connection3);
                                    connection3.Open();
                                    command3.ExecuteNonQuery();
                            }
                        }
                        //vvvvv VARIABLE THAT NEEDS TO BE BOUND vvvvvvv
                        Binder.daWindow.progUpdate = ((100.00 / databaseTables.Length) * dbTIndex);
                    }

(这部分显然有更多代码,如果需要我可以提供)

提前致谢!

编辑:

对于 XAML,local.progUpdate 不是我尝试过的唯一方法。我自己完成了路径、progUpdate 等。

标签: wpfbindingprogress-bar

解决方案


您的 ProgressBar 的第一个绑定没有意义:{Binding local.progUpdate, UpdateSourceTrigger=PropertyChanged}. 您没有指定源,也没有DataContext在任何地方分配,因此绑定不会评估任何内容,因为路径毫无意义。因为该属性位于窗口本身,所以您可以将窗口的数据上下文分配给它自己。

// DateWindow.xaml.cs
public DateWindow()
{
    InitializeComponent();
    DataContext = this;
    ...
}

<!-- DateWindow.xaml -->
<ProgressBar Value="{Binding progUpdate, Mode=OneWay}" ... />

您的其他绑定将需要mx并且mn是属性而不是字段。


推荐阅读