How do I check on the properties of WPF elements w

2019-09-01 02:12发布

I am following a bunch of different examples etc and trying to do my own thing with that.

Essentially, in my code behind, I have a separate class. I think this is being used for MVVM, though I have almost no idea what that kind of means (I get the point, but it looks confusing!).

Anyway... Here is my code in a seperate class (same namespace) to the code behind for my WPF form:

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

Now, I have a textblock on my WPF xaml page, named AccountType. The text within this can cycle between three different values, and I need to change the string query at the end of that code block above based on that. If it is one value, the string = x. if its the second value, the string = y etc...

That part of the class is used to populate a combobox with entries from a database. Will changing that string then automatically force the combobox to update? Or will I ned to do something else after this also?

As requested, here is my XAML (the relevant stuff anyway)

<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>

Full 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.
        }
    }

}

标签: c# wpf xaml mvvm
1条回答
叼着烟拽天下
2楼-- · 2019-09-01 02:53

Firstly, in your ViewModel, you can create a property for your AccountType textblock to bind to:

private string _AccountType;

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

            //OnPropertyChanged stuff here.
        }
    }

When you cycle the account types, update this property, it will also update the UI.

You first need to set the binding on your TextBlock:

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

Then you can simply use this property in your query.

That part of the class is used to populate a combobox with entries from a database. Will changing that string then automatically force the combobox to update? Or will I ned to do something else after this also?

It's good practice to use an ObservableCollection for lists that you are binding to from your UI. If you are not using this already, then please do so, as it handles the property changed stuff for you, and will update the bindings when an item is added/removed from the list.

Please let me know if I have missed anything.

查看更多
登录 后发表回答