MVVM ComboBox Text绑定正常但ID不绑定 [英] MVVM ComboBox Text binds fine but ID does not

查看:60
本文介绍了MVVM ComboBox Text绑定正常但ID不绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

===================================================

xaml

===================================================
    <Window.Resources>
        <local:Database2 x:Key="db2" />
        <local:Server x:Key="sv" />
        <viewmodel:MainWindowViewModel x:Key="vm"></viewmodel:MainWindowViewModel>
        <Style TargetType="TextBlock" x:Key="mouseOverStyle">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>

            <Grid Margin="0 10" HorizontalAlignment="Center">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                    <ColumnDefinition Width="100"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Text="Source Server" FontWeight="Bold" Grid.Column="0" HorizontalAlignment="Right" Margin="0 15 10 0"/>
                <ComboBox Grid.Column="1" Margin="0 10 0 0" Height="20" Name="cmbSrcServer" Tag="Server" TabIndex="10"  DisplayMemberPath="ServerName"
                          ItemsSource="{Binding lstServer, Source={StaticResource vm}}"
                          Text="{Binding sv.ServerName, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}"
                          SelectedItem="{Binding sv.ServerID, Source={StaticResource vm}, UpdateSourceTrigger=PropertyChanged}">
                </ComboBox>
            </Grid>

====================================================
Server class
====================================================
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace FTPFileMaintWPF_MVVM
{
    public class Server : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private int _ServerID;
        private int _MinimalFTPMinutes;
        //private FontSize(int i) { val = i; }
        //private int val;
        private string _ServerName;
        private string _SrcFile;
        private string _SrcPath;
        private string _DestPath;
        //private FontSize _DestPathFontSize;

        public Server(int intServerID, string strServerName)
        {
            ServerID = intServerID;
            ServerName = strServerName;
        }


        public int ServerID
        {
            get
            {
                return _ServerID;
            }
            set
            {
                _ServerID = value;
                OnPropertyChanged("ServerID");
            }
        }

        public string ServerName
        {
            get
            {
                return _ServerName;
            }
            set
            {
                _ServerName = value;
                OnPropertyChanged("ServerName");
            }
        }


        protected void OnPropertyChanged([CallerMemberName] string name = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

===================================================

ViewModel

===================================================
        ICommand _commandSrc, _commandDest, _commandSave;
        ICommand _mouseOverFileName, _mouseOverFilePath, _mouseOverDestPath;
        Database2 db = new Database2();
        //private string _MouseActionLabel;
        private static string _vmSrcPathAndFile, _vmDestPath;
        public Server sv { get; set; }
        public MouseEvents me { get; set; }
        //MainWindowViewModel vm;

        public List<Server> lstServer { get { return FillDropDown(); } }

        public string SrcFolderCaption { get; set; }
        public int MinimalFTPMinutes { get; set; }

        public MainWindowViewModel()
        {
            SrcFolderCaption = "Select Source File";
            sv = new Server(1, "1123");
        }


        private bool CanExecuteSave(object parameter)
        {
            return true;
        }

        private void ExecuteSave(object parameter)
        {
            int intPos;

            intPos = vmSrcPathAndFile.IndexOf('|');
            sv.SrcFile = vmSrcPathAndFile.Substring(intPos + 1, vmSrcPathAndFile.Length - intPos - 1);
            sv.SrcPath = vmSrcPathAndFile.Substring(0, intPos);
            db.SaveFTPFileData(sv.SrcFile, sv.SrcPath, vmDestPath, MinimalFTPMinutes, "", sv.ServerID);
        }


        private List<Server> FillDropDown()
        {
            List<Server> lst = new List<Server>();
            DataSet ds = new DataSet();
            Database2 db = new Database2();
            ArrayList alName = null;
            ArrayList alVal = null;
            ArrayList alDirec = null;

            ds = new DataSet();
            ds = db.FillDropDowns("spSelectServersForDropdown", ds, alName, alVal, alDirec);

            //sv.ServerID = 0;
            return ds.Tables[0].AsEnumerable().Select(dataRow => new Server(sv.ServerID, sv.ServerName) { ServerID = dataRow.Field<int>(0), ServerName = dataRow.Field<string>(1) }).ToList();
        }

======================================================
FTPSaveButtonCommand class
======================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace FTPFileMaintWPF_MVVM.Models
{
    internal class FTPSaveButtonClick : ICommand
    {
        Predicate<object> canExecute;
        Action<object> execute;

        public FTPSaveButtonClick(Predicate<object> _canexecute, Action<object> _execute)
        {
            canExecute = _canexecute;
            execute = _execute;
        }

        #region ICommand Members
        public bool CanExecute(object parameter)
        {
            return canExecute == null ? true : canExecute(parameter);
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            execute(parameter);
        }
        #endregion
    }
}




推荐答案

嗨  OldeEnglishD,

Hi OldeEnglishD,

根据说明,问题与WPF更相关。我将把线程移到论坛:
Windows Presentation Foundation(WPF) )。会有更专业的人为你解决这个问题。

According to the description, the issue is much more related to WPF. I will move the thread to the forum: Windows Presentation Foundation (WPF). There will be someone more professional to solve this problem for you.

问候,

Kyle


这篇关于MVVM ComboBox Text绑定正常但ID不绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆