使用WPF中的ivalueconverter将数据从字符串转换为复选框 [英] Convert data from string to checkbox with ivalueconverter in WPF
问题描述
我的应用有问题。我用WPF构建了一个应用程序。我有一个数据是从DB导入的observablecollection,这个数据的一部分(一个属性)是字符串,我想将这个数据绑定到复选框这个字符串数据看起来像
(abc_abcd / abc_abcd / abc_abcd .....)
每个abc_abcd它应该链接到一个Checkbox(这个字符串是用户授权,它作为一个字符串存储在数据库中但是当我想将它绑定到复选框时我应该溢出然后将它与每个Checkbox的静态值进行比较,以了解是否应该检查tjis Checkbox)。我知道我应该使用ivalueconverter,但我不知道如何制作它。任何解决方案请......
class 运算符
{
public string operatorname { get ; set ; }
public string operatorpassword { get 跨度>; set ; }
public string authorization { get 跨度>; set ; }
public Boolean renew_password { get 跨度>; set ; }
public int interval_of_change { get 跨度>; set ; }
public int password_min_length { get 跨度>; set ; }
public DateTime last_change { get ; set ; }
}
class Databasecon
{
// 数据库的第一次绑定
public ObservableCollection< Operator>运算符{ get ; private set ; }
public Databasecon()
{
this .operators = new ObservableCollection< Operator>();
}
public void Datacon(< span class =code-keyword> string conn)
{
MySqlConnection con = null ;
MySqlCommand com = null ;
MySqlDataReader myreader = null ;
int columnOrdinaloperatorname = -1;
int columnOrdinalauthorisation = -1;
con = new MySqlConnection(conn);
尝试
{
如果(com == null )
{
com = new MySqlCommand( SELECT * FROM运算符,con);
com.Connection.Open();
myreader = com.ExecuteReader();
columnOrdinaloperatorname = myreader.GetOrdinal( operator_name);
columnOrdinalauthorisation = myreader.GetOrdinal( authorization);
while (myreader.Read())
{
this .operators.Add( new Operator(){
operatorname = myreader.GetString(columnOrdinaloperatorname).ToString(),
// DB中的授权可以为空
authorization =(columnOrdinalauthorisation > = 0 )?(myreader.IsDBNull(columnOrdinalauthorisation)? string .Empty:myreader.GetString(columnOrdinalauthorisation)): string .Empty
});
}
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
最后
{
if (myreader!= null )
myreader.Close();
if (com!= null )
{
if (com.Connection!= null )
com.Connection.Close() ;
}
}
}
}
pre>
< pre lang = c# > < /
< span class =code-keyword>
class collect : INotifyPropertyChanged
{
string myconn = < span class =code-keyword> SERVER = localhost; PORT = 3306; UID = TestUser; PASSWORD = TestUser; DATABASE = heitel_db_v011 ;
private Databasecon databasecon = null;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand Bindingcommand { get; set; }
public < span class =code-keyword> ObservableCollection<运算符 > 运算符
{
get
{
if (此 .databasecon.operators!= null )
{
返回 < span class =code-keyword> this .databasecon.operators;
}
else
{
return null ;
}
}
set
{
此 .operators = value ; RaisePropertyChanged( operator);
}
}
private void RaisePropertyChanged( string propertyName)
{
var handler = 此跨度> .PropertyChanged;
if (handler!= null )
{
handler( this , new PropertyChangedEventArgs(propertyName));
}
}
public collect()
{
< span class =code-keyword> this .databasecon = new Databasecon();
Bindingcommand = new Relaycommands(()= > databasecon.Datacon(myconn));
}
}
类 Relaycommands:ICommand
{
public event EventHandler CanExecuteChanged;
private Action _command;
public Relaycommands(Action command)
{
_command = command;
}
public bool CanExecute( object 参数)
{
return true ;
}
public void 执行( object 参数)
{
if (_command!= null )
_command();
}
}
我的尝试:
我试着写转换器,但我不能。我不知道什么时候应该将字符串数据溢出到单词并将其保存在数组
你好,
这应该有所帮助:
如何:使用String.Split解析字符串(C#编程指南) [ ^ ]
和
http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter / [ ^
I have a Problem with my app. I build an app with WPF. I have a data it is imported from DB with observablecollection, part of this data (one properity) is string and I want to bind this data to checkboxes this string data Looks like
(abc_abcd/abc_abcd/abc_abcd.....)
every abc_abcd it should be linked to one Checkbox( this string is user authorisations and it stored in the data base as one string but when i want to bind it to checkboxes i should spilt it and then compare it with static value for every Checkbox to know if tjis Checkbox should be checked or not ). I know that I should use ivalueconverter but I don't have an idea how to make it. any Solution please......
class Operator
{
public string operatorname { get ; set; }
public string operatorpassword { get; set; }
public string authorisation { get; set; }
public Boolean renew_password { get; set; }
public int interval_of_change { get; set; }
public int password_min_length { get; set; }
public DateTime last_change { get; set; }
}
class Databasecon
{
// First Binding for the Database
public ObservableCollection<Operator> operators { get; private set; }
public Databasecon()
{
this.operators = new ObservableCollection<Operator>();
}
public void Datacon(string conn)
{
MySqlConnection con = null;
MySqlCommand com = null;
MySqlDataReader myreader = null;
int columnOrdinaloperatorname = -1;
int columnOrdinalauthorisation = -1;
con = new MySqlConnection(conn);
try
{
if (com == null)
{
com = new MySqlCommand("SELECT * FROM operators", con);
com.Connection.Open();
myreader = com.ExecuteReader();
columnOrdinaloperatorname = myreader.GetOrdinal("operator_name");
columnOrdinalauthorisation = myreader.GetOrdinal("authorisation");
while (myreader.Read())
{
this.operators.Add(new Operator() {
operatorname = myreader.GetString(columnOrdinaloperatorname).ToString(),
// authorisation can be Null in DB
authorisation = (columnOrdinalauthorisation >= 0) ? (myreader.IsDBNull(columnOrdinalauthorisation) ? string.Empty : myreader.GetString(columnOrdinalauthorisation)) : string.Empty
});
}
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (myreader != null)
myreader.Close();
if (com != null)
{
if (com.Connection != null)
com.Connection.Close();
}
}
}
}
pre>
<pre lang="c#"></
class collect : INotifyPropertyChanged
{
string myconn = "SERVER=localhost;PORT=3306;UID=TestUser;PASSWORD=TestUser;DATABASE=heitel_db_v011";
private Databasecon databasecon = null;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand Bindingcommand { get; set; }
public ObservableCollection<Operator> operators
{
get
{
if (this.databasecon.operators != null)
{
return this.databasecon.operators;
}
else
{
return null;
}
}
set
{
this.operators = value; RaisePropertyChanged("operators");
}
}
private void RaisePropertyChanged(string propertyName)
{
var handler = this.PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public collect()
{
this.databasecon = new Databasecon();
Bindingcommand = new Relaycommands(() => databasecon.Datacon(myconn));
}
}
class Relaycommands : ICommand
{
public event EventHandler CanExecuteChanged;
private Action _command;
public Relaycommands(Action command)
{
_command = command;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
if (_command != null)
_command();
}
}
What I have tried:
I try to write the converter but I couldn't. I don't know when I should Spill the string data to words and save it in an Array
Hello,
this should help:
How to: Parse Strings Using String.Split (C# Programming Guide)[^]
and
http://www.wpf-tutorial.com/data-binding/value-conversion-with-ivalueconverter/[^]
这篇关于使用WPF中的ivalueconverter将数据从字符串转换为复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!