none
代码绑定问题 RRS feed

  • 问题

  • bindingWidth.Source = grid; bindingWidth.Path = new Windows.UI.Xaml.PropertyPath("Width"); bindingWidth.Mode = BindingMode.OneWay;

    上面的grid是一个Grid,我要把一个直线的y2绑定到grid宽的一半,怎样实现

    lineLeft.X1 = 0;
                            lineLeft.Y1 = 0;
                            lineLeft.X2 = 0;
                            //lineLeft.Y2 = this.Height;
                            lineLeft.SetBinding(Line.Y2Property, bindingHeight);

    2013年12月20日 9:43

答案

  • Hi runqian,

    我认为你需要一个Convert Class来帮助你转变长度,把下面这段代码放入到你的项目中。

        public class lineWidthConvert : IValueConverter
        {
            public object Convert(object value, Type targetType,
                object parameter, string language)
            {
                return (double)value / 2;
            }
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }

    在你的binding code中做如下修改,即可实现你想要的效果:

                Binding bindingWidth = new Binding();
                bindingWidth.Source = grid;
                bindingWidth.Path = new Windows.UI.Xaml.PropertyPath("Width");
                bindingWidth.Mode = BindingMode.OneWay;
                bindingWidth.Converter = new lineWidthConvert();
    
                lineLeft.X1 = 0;
                lineLeft.Y1 = 0;
                lineLeft.X2 = 0;
                //lineLeft.Y2 = this.Height;
                lineLeft.SetBinding(Line.Y2Property, bindingWidth);

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 runqian 2013年12月23日 2:07
    2013年12月22日 4:09
    版主

全部回复

  • Hi runqian,

    我认为你需要一个Convert Class来帮助你转变长度,把下面这段代码放入到你的项目中。

        public class lineWidthConvert : IValueConverter
        {
            public object Convert(object value, Type targetType,
                object parameter, string language)
            {
                return (double)value / 2;
            }
            public object ConvertBack(object value, Type targetType,
                object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }

    在你的binding code中做如下修改,即可实现你想要的效果:

                Binding bindingWidth = new Binding();
                bindingWidth.Source = grid;
                bindingWidth.Path = new Windows.UI.Xaml.PropertyPath("Width");
                bindingWidth.Mode = BindingMode.OneWay;
                bindingWidth.Converter = new lineWidthConvert();
    
                lineLeft.X1 = 0;
                lineLeft.Y1 = 0;
                lineLeft.X2 = 0;
                //lineLeft.Y2 = this.Height;
                lineLeft.SetBinding(Line.Y2Property, bindingWidth);

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 runqian 2013年12月23日 2:07
    2013年12月22日 4:09
    版主
  • thanks, It works
    2013年12月23日 2:07
  • 但是我现在有个问题,我想定义一个Bingding对象,想在多处使用,怎们会出错?

      Binding binding = new Binding();
                    binding.Source = this.grid;
                    binding.Path = new PropertyPath("Height");
                    lineWidth.SetBinding(Line.Y2Property, binding);
    
    
                    //Binding bindingHeight = new Binding();
                    //binding.Source = this.grid;
                    binding.Path = new PropertyPath("Width");
                    //binding.Converter = new lineWidthConvert();
    
    
                    //lineWidth.X2 = this.grid.Width;
                    lineWidth.SetBinding(Line.X2Property, binding);
    运行时会出COMException异常,不知道怎样解决?

    2013年12月23日 3:23
  • Hi runqian,

    Binding对象只处理一个,所以你需要新建一个Binding对象用于绑定Width。你可以试想如果同一个Binding应用到两个对象上去,如果一旦发生binding源发生变化,这两个对象如何处理binding?

    所以你应该分开写这两个Binding。

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年12月23日 6:10
    版主