WPF MultiBinding 给数据目标赋值后绑定无效的问题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chifuqi/article/details/10585525

近来用WPF制作做了一个IP控件的试验,IP数值通过TextBox输入(也可以定制0-254的输入),需要4个TextBox多路绑定至System.Net.IPAddress,如图前4个TextBox作为数值输入,多路绑定至IPAddress, 最后一个TextBox绑定了IPAddress,且依赖项属性IPAddressProperty 变化时会引发函数IPAddressChangedCallback,从而改变前4个TextBox。

前4个TextBox的多路绑定以TextBox为数据源,IPAddress为数据目标

后一个TextBox的绑定以IPAddress为数据源

结果如下:

1、改变前4个TextBox任意一个值都会引起最后一个TextBox的变化。

2、改变最后一个TextBox也会导致对应前4个TextBox的变化。

3、一旦改变了最后一个TextBox的数值,即更改了IPAddress,则前4个TextBox的绑定便会无效,即无法改变IPAddress。

4、在程序中主动设置IPAddress也会导致多路绑定无效,判定方法为多路绑定的转换函数不执行。

总结:多路绑定中改变数据目标后,会导致多路绑定无效。具体原因不明,请大牛指点。

代码:http://download.csdn.net/detail/chifuqi/6039159

展开阅读全文

WPF,这个双向的MultiBinding绑定,反向为什么不见效

12-04

[code=html]rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn rnrn[/code]rnrn[code=csharp]rnprivate void Button_Click(object sender, RoutedEventArgs e)rnrn textblock1.Text = "张三";rn Number m = this.FindResource("number") as Number;rn MessageBox.Show(m.Number1.ToString());rnrn[/code]rn[code=csharp]rnclass Numberrnrn public int Number1 get; set; rn public int Number2 get; set; rnrn[/code]rn[code=csharp]rnclass NumberConverter : IMultiValueConverter rnrn public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) //返回值即作为目标属性值rn rn string number;rn switch ((string)parameter)rn rn case "乘一下":rn number = ((Int32)values[0] * (Int32)values[1]).ToString(); //相乘rn break;rn case "减一下":rn number = ((Int32)values[1] - (Int32)values[0]).ToString();//第二个数减第一个数rn break;rn default:rn number = ((Int32)values[0] + (Int32)values[1]).ToString(); //相加rn break;rn rn return number;rn rn public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)rn rn return new object[2]2,1;rn rnrn[/code]rn[img=https://img-bbs.csdn.net/upload/201312/04/1386165139_512471.jpg][/img]rn上面的示例,将TextBlock的Text属性绑定到一个对象,使用的是MultiBinding绑定,并把Mode设置为TwoWay。可是结果是,单向绑定能正常显示,单击button之后,更改TextBlock的Text属性值,并没有任何响应呢?哪里出错了呢? 论坛

给实体类的数据赋值

12-29

[code=csharp]rn protected void Page_Load(object sender, EventArgs e)rn rn if (Session["UserName"] == null)rn rn //this.ClientScript.RegisterClientScriptBlock(this.GetType(), "key1", "alert('请先登录!')",true);rn //Response.Redirect("~/index.aspx");rn this.ClientScript.RegisterStartupScript(this.GetType(), "", " ");rnrn rn elsern rn if (!IsPostBack)rn rn Profile.shoplist.GetCartItem(Session["UserName"].ToString());rn string DressId = Request.QueryString["DressId"];rn int Num = Convert.ToInt32(Request.QueryString["num"]);rn string size = Request.QueryString["size"];rn if (!string.IsNullOrEmpty(DressId))rn rnrn if (Session["flag"].ToString().Equals(""))rn rn Profile.shoplist.Add(DressId, size, Num);rn Session["flag"] = DressId;rnrn rnrn rn BingCart(Profile.shoplist);rn Profile.shoplist.SetCartItems(Session["UserName"].ToString());rn Profile.Save();rn rn rnrn rn[/code]rn[code=csharp]rn public class DressItemInforn rn string dressId;//服装编号rn public string DressIdrn rn get return dressId; rn set dressId = value; rn rnrn string dressMerchant;//商家rn public string DressMerchantrn rn get return dressMerchant; rn set dressMerchant = value; rn rnrn decimal unitcost;//成本价rn public decimal Unitcostrn rn get return unitcost; rn set unitcost = value; rn rnrn decimal marketPrice;//市场价rn public decimal MarketPricern rn get return marketPrice; rn set marketPrice = value; rn rnrn decimal orignial;//吊牌价rn public decimal Orignialrn rn get return orignial; rn set orignial = value; rn rnrn string dressTitle;//商品标题rn public string DressTitlern rn get return dressTitle; rn set dressTitle = value; rn rnrn string dressdetails;//商品介绍rn public string Dressdetailsrn rn get return dressdetails; rn set dressdetails = value; rn rnrn string dressImage;//图片rn public string DressImagern rn get return dressImage; rn set dressImage = value; rn rnrn int qty;//库存rn public int Qtyrn rn get return qty; rn set qty = value; rn rnrn string size;//尺寸大小rnrn public string Sizern rn get return size; rn set size = value; rn rn int quantity;//购买数量rnrn public int Quantityrn rn get return quantity; rn set quantity = value; rn rn ///rn ///默认构造rn ///rn public DressItemInfo() rn public DressItemInfo(int quantity) this.quantity = quantity; rn ///rn ///带参数构造rn ///rn public DressItemInfo(string DressId, string DressMerchant, decimal Unitcost, decimal MarketPrice, decimal Orignial, string DressTitle, string Dressdetails, string DressImage, int Qty,string Size,int Quantiy)rn rn this.dressId = DressId;rn this.dressMerchant = DressMerchant;rn this.unitcost = Unitcost;rn this.marketPrice = MarketPrice;rn this.orignial = Orignial;rn this.dressTitle = DressTitle;rn this.dressdetails = Dressdetails;rn this.dressImage = DressImage;rn this.qty = Qty;rn this.size = Size;rn this.quantity = Quantiy;rn rn rn[/code]rnrn我希望加载的时候把Num赋值给DressItemInfo的Quantity 要怎么实现?因为我下面的方法会调用到Quantityrn另外顺便问下 在加载的时候我注释的那2行 我发现当UserName为null的时候不会出现提示 而是直接跳转到首页 //Response.Redirect("~/index.aspx");这句没有的话就会跳出提示 论坛

没有更多推荐了,返回首页