如何在不同的课程中检查WPF元素的属性?

时间:2015-03-06 13:57:55

标签: c# wpf xaml mvvm

我正在关注一系列不同的例子,并尝试用它来做我自己的事情。

基本上,在我的代码背后,我有一个单独的类。我认为这是用于MVVM,虽然我几乎不知道这是什么意思(我明白了,但看起来很混乱!)。

无论如何......这是我在WPF表单背后的代码中的一个单独的类(相同的命名空间)中的代码:

public class ChartViewModel
    {
        public ChartViewModel()
        {
            DataTable dataNames = new DataTable();
            try
            {
                var db = new SQLiteDatabase();
                string query = "SELECT DISTINCT Name FROM Main";
                ...

现在,我的WPF xaml页面上有一个文本块,名为AccountType。其中的文本可以在三个不同的值之间循环,我需要在此基础上更改上面代码块末尾的字符串查询。如果是一个值,则string = x。如果它是第二个值,则字符串= y等......

该类的一部分用于使用数据库中的条目填充组合框。更改该字符串会自动强制组合框更新吗?或者我也会在此之后再做其他事情吗?

根据要求,这是我的XAML(无论如何相关的东西)

<ComboBox x:Name="NameBox" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{Binding Names}" Width="100" />
<Grid Grid.Row="0" Grid.Column="1">
    <ProgressBar x:Name="AccountTypeProgress" Width="70" Height="20" MouseDown="AccountTypeButton_Click" />
    <TextBlock x:Name="AccountType" Text="Main" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="#FF252525" FontSize="16" FontWeight="Bold" MouseDown="AccountTypeButton_Click" />
</Grid>

完整ViewModel

public class ChartViewModel
{
    public ChartViewModel()
    {
        AccountType = "Main";

        DataTable dataNames = new DataTable();
        try
        {
            var db = new SQLiteDatabase();
            string query = "SELECT DISTINCT Name FROM Main";

            dataNames = db.GetDataTable(query);
        }
        catch (Exception fail)
        {
            string error = "The following error has occured:\n\n";
            //error += fail.Message.ToString() + "\n\n";
            //System.Windows.MessageBox.Show(error);
        }

        this.Names = new ObservableCollection<string> { };

        foreach (DataRow row in dataNames.Rows)
        {
            string name = row["Name"].ToString();
            Names.Add(name);
        }


        int x = 42067;

        DataTable datastats = new DataTable();
        try
        {
            var db = new SQLiteDatabase();
            String query = "SELECT * FROM Main WHERE Name = \"9n\"";
            datastats = db.GetDataTable(query);
        }
        catch (Exception fail)
        {
            String error = "The following error has occurred:\n\n";
            //error += fail.Message.ToString() + "\n\n";
            //System.Windows.MessageBox.Show(error);
        }

        this.overall = new List<DataPoint>
        {
            //foreach(DataRow row in datastats.Rows)
            //{

            //}
        };

        foreach (DataRow row in datastats.Rows)
        {
            DateTime sDate = DateTime.Parse(row["Date"].ToString());
            //int date = ((sDate.Year - 1900)) + sDate.Day;
            sDate = sDate.Date;
            double date = sDate.ToOADate() - 1;
            int dateint = Convert.ToInt32(date);

            string sLevel = row["OverallL"].ToString();
            int level = Convert.ToInt32(sLevel);

            this.overall.Add(new DataPoint(date, level));

        }

        this.overall.Add(new DataPoint(42100, 10));
        this.overall.Add(new DataPoint(42110, 10));

        this.Title2 = "Example 2";
        this.Points = new List<DataPoint>
            {
                new DataPoint(x, 4),
                new DataPoint(42077, 13),
                new DataPoint(42087, 15),
                new DataPoint(42097, 16),
                new DataPoint(42107, 12),
                new DataPoint(42117, 12)
            };
        this.Data = new List<DataPoint>
            {
                new DataPoint(42067, 10),
                new DataPoint(42077, 15),
                new DataPoint(42087, 16),
                new DataPoint(42097, 20),
                new DataPoint(42107, 10),
                new DataPoint(42117, 12)
            };
    }


    public string Title2 { get; private set; }

    public IList<DataPoint> Points { get; private set; }
    public IList<DataPoint> Data { get; private set; }
    public IList<DataPoint> overall { get; private set; }

    public ObservableCollection<string> Names { get; private set; }

    private string _AccountType;

    public string AccountType
    {
        get { return _AccountType; }
        set
        {
            _AccountType = value;

            //OnPropertyChanged stuff here.
        }
    }

}

1 个答案:

答案 0 :(得分:3)

首先,在ViewModel中,您可以为AccountType文本块创建一个属性以绑定到:

private string _AccountType;

    public string AccountType
    {
        get { return _AccountType; }
        set 
        { 
            _AccountType = value; 

            //OnPropertyChanged stuff here.
        }
    }

当您循环帐户类型时,更新此属性,它也会更新用户界面。

首先需要在TextBlock上设置绑定:

<TextBlock Text="{Binding AccountType}" ... />

然后,您只需在查询中使用此属性即可。

该类的一部分用于使用数据库中的条目填充组合框。更改该字符串会自动强制组合框更新吗?或者我也会在此之后再做其他事情吗?

将ObservableCollection用于从UI绑定的列表是一种很好的做法。如果您尚未使用此功能,请执行此操作,因为它会为您处理属性更改的内容,并在列表中添加/删除项目时更新绑定。

如果我错过了什么,请告诉我。