【问题标题】:combo box and chart visibility (mvvm and wpf)组合框和图表可见性(mvvm 和 wpf)
【发布时间】:2014-08-30 10:04:14
【问题描述】:

我有一个组合框和一个要显示的图表(条形图)(按 mvvm 模式)。我想做的是::

-将数据从数据库加载到组合框(绑定)-(完成)

-加载到combo box后,从combobox中选择后,在查询中选择combobox值后只会显示图表(未完成)。它不会在组合框的选择更改时更新

我无法获得正确的流程

视图模型文件

namespace charting
{
    class fbvm : ViewModelBase, INotifyPropertyChanged
    {
        public String eID;

        private List<KeyValuePair<string, float>> _chartData;
        public List<KeyValuePair<string, float>> ChartData
        {
            get
            {
                return _chartData;
            }
            set
            {
                _chartData = value;
                OnPropertyChanged(() => ChartData);
            }
        }
        private List<string> _MyComboBoxData;
        public List<string> MyComboBoxData
        {
            get
            {
                return _MyComboBoxData;
            }
            set
            {
                _MyComboBoxData = value;
                OnPropertyChanged(() => MyComboBoxData);
            }
        }

        private Boolean _loadoncbsel;
        public Boolean loadoncbsel
        {         
            get
            {
                return _loadoncbsel;

            }
            set
            {            
                _loadoncbsel = value;
                OnPropertyChanged(() => loadoncbsel);
            }

        }

        private string _selectedcb;
        public string selectedcb
        {
            get
            {

                return _selectedcb;

            }
            set
            {              
                _selectedcb = value;
                 OnPropertyChanged(() => selectedcb);
                 if (value == null)
                     _loadoncbsel = false;
                 else
                     _loadoncbsel = true;
            }

        }

        public fbvm()
        {
            MyComboBoxData = new List<string>();
            comboboxload();
            ChartData = new List<KeyValuePair<string, float>>();

        }

         private void comboboxload()
         {
             OleDbConnection ConDb;

             ConDb = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Admin\\Documents\\Visual Studio 2012\\Projects\\Feedback\\Feedback.accdb");

             try
             {
                 ConDb.Open();
                 OleDbCommand DBSelect = new System.Data.OleDb.OleDbCommand("select FName, LName,ID_NAME from NameList", ConDb);
                 OleDbDataReader reader = DBSelect.ExecuteReader();
                 while (reader.Read())
                 {
                     string eNAME = "";
                     eID = reader["ID_NAME"].ToString();
                     eNAME += reader["FName"].ToString();
                     eNAME += " " + reader["LName"].ToString();

                     MyComboBoxData.Add(eNAME);
                 }
             }
             catch (Exception ae)
             {
                 MessageBox.Show(ae.Message);

             }//catch




         }
        private void LoadColumnChartData()
        {
            int cc1=0,tc1=0,aa1=0,blfe1=0,count=0;
            float cc11 = 0, tc11 = 0, aa11 = 0, blfe11 = 0;
            int year;
            OleDbConnection connect = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Admin\Documents\Visual Studio 2012\Projects\Feedback\Feedback.accdb");
            connect.Open();
            string query = "select CC,TC,AA,BLFE,WMU from "+selectedcb;
            OleDbCommand select = new OleDbCommand("select", connect);
            select.CommandText = query;
            OleDbDataReader reader = select.ExecuteReader();
            while (reader.Read())
            {
                cc1 += Int32.Parse(reader[0].ToString());
                tc1 += Int32.Parse(reader[1].ToString());
                aa1 += Int32.Parse(reader[2].ToString());
                blfe1 += Int32.Parse(reader[3].ToString());
                ++count;
            }
            cc11 =(float) cc1 / count; aa11 =(float) aa1 / count;
            tc11 =(float) tc1 / count; blfe11 =(float) blfe1 / count;
         //   cc11 = 3.11f;
         //   MessageBox.Show(cc11.ToString(), tc11.ToString());
            ChartData.Add(new KeyValuePair<string, float>("cc", cc11));
            ChartData.Add(new KeyValuePair<string, float>("tc", tc11));
            ChartData.Add(new KeyValuePair<string, float>("aa", aa11));
            ChartData.Add(new KeyValuePair<string, float>("blfe", blfe11));
            ChartData = new List<KeyValuePair<string, float>>(ChartData);

        }//loadcoloumnchart


        #region INotifyPropertyChanged Members

        /// <summary>
        /// Need to implement this interface in order to get data binding
        /// to work properly.
        /// </summary>
        /// <param name="propertyName"></param>
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }//class fbvm
}//namespace

xaml 文件

<Window x:Class="charting.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:DV="clr-namespace:System.Windows.Controls.DataVisualization;assembly=System.Windows.Controls.DataVisualization.Toolkit"
    xmlns:DVC="clr-namespace:System.Windows.Controls.DataVisualization.Charting;assembly=System.Windows.Controls.DataVisualization.Toolkit"

    Title="MainWindow" Height="350" Width="816.045">
<Window.Resources>
    <BooleanToVisibilityConverter x:Key="btov"/>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="83*"/>
        <ColumnDefinition Width="434*"/>
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <Label > Select Name:</Label>
    </StackPanel>
    <StackPanel Grid.Column="1" >
        <ComboBox  x:Name="SelectNameCB"  SelectedValue="{Binding selectedcb, Mode=TwoWay}"  FontSize="15" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" ItemsSource="{Binding MyComboBoxData}" />

        <DVC:Chart Canvas.Top="80" Canvas.Left="10" Name="mcChart"  Margin="10,10,31,0" Height="250" 
       Background="LightGoldenrodYellow"  Title="{Binding Text, ElementName=SelectNameCB}" Visibility="{Binding loadoncbsel, Converter={StaticResource btov}}" >
            <DVC:Chart.Series>
                <DVC:BarSeries   Title="Avg. Score" IndependentValueBinding="{Binding Path=Key}" DependentValueBinding="{Binding Path=Value}" ItemsSource="{Binding ChartData}" Margin="10,10,76,10" AnimationSequence="LastToFirst">
                </DVC:BarSeries>

            </DVC:Chart.Series>
        </DVC:Chart>


    </StackPanel>
</Grid>

【问题讨论】:

    标签: c# wpf mvvm charts combobox


    【解决方案1】:

    组合框: <ComboBox x:Name="SelectNameCB" FontSize="15" Margin="10,10,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Width="207" ItemsSource="{Binding MyComboBoxData}" SelectedItem="{Binding Indexname, Mode=TwoWay}" />

    `

    【讨论】:

      【解决方案2】:

      尝试绑定SelectedItem 属性而不是SelectedValue

      <ComboBox  x:Name="SelectNameCB"  
                 SelectedItem="{Binding selectedcb, Mode=TwoWay}"  
                 ItemsSource="{Binding MyComboBoxData}" 
                 ...... />
      

      然后在设置selectedcb 属性后,通过调用LoadColumnChartData() 填充图表:

      public string selectedcb
      {
          get{...}
          set
          {              
              _selectedcb = value;
               OnPropertyChanged(() => selectedcb);
               if (value == null)
                   _loadoncbsel = false;
               else
               {
                   _loadoncbsel = true;
                   LoadColumnChartData();
               }
          }
      }
      

      【讨论】:

      • “不工作”永远不足以诊断问题,更不用说修复它
      • 对不起@har07 我正在处理那个......它仍然没有显示图表。我检查了参数值.. _loadoncbsel 永远不会更新。
      • 组合框怎么样,是否正确填充?
      • 是的。组合框已正确填充。我只是在检查,它没有更新 _loadoncbsel 的真实值
      • 如果你在selectedcb的设置器处设置断点,它会命中吗?如果是,那么_loadoncbsel 的设置器会被调用吗?输出窗口中是否有任何绑定错误消息?
      猜你喜欢
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 2014-03-30
      • 2012-02-29
      相关资源
      最近更新 更多