public object ProvideValue(IMarkupExtensionContext context)
 {
     Object realSource = Source ?? context.DataContext;
     if ( null != realSource && !( realSource is INotifyPropertyChanged ) ) {
         throw new ArgumentException("Source must be INotifyPropertyChanged to use bindings");
     }
     if (null != realSource) {
         BindingMode mode = BindingMode.Default;
         if ( Path != null ) {
             Type enumType = typeof ( BindingMode );
             string[ ] enumNames = enumType.GetEnumNames( );
             for ( int i = 0, len = enumNames.Length; i < len; i++ ) {
                 if ( enumNames[ i ] == Mode ) {
                     mode = ( BindingMode ) Enum.ToObject( enumType, enumType.GetEnumValues( ).GetValue( i ) );
                     break;
                 }
             }
         }
         BindingBase binding = new BindingBase( context.Object, context.PropertyName,
             (INotifyPropertyChanged) realSource, Path, mode);
         if ( Converter != null )
             binding.Converter = Converter;
         binding.Bind(  );
         // mb return actual property value ?
         return null;
     }
     return null;
 }
//        [TestMethod]
        public void TestString()
        {
            SourceClass source = new SourceClass();
            TargetClass target = new TargetClass();
            BindingBase binding = new BindingBase(target, "Title", source, "Text", BindingMode.OneWay);
            binding.Bind();
            source.Text = "Text!";
            Assert.AreEqual( target.Title, source.Text );
        }
 public void TestListBinding2() {
     SourceClass source = new SourceClass();
     TargetClass target = new TargetClass();
     BindingBase binding = new BindingBase(target, "Items", source, "SourceItems", BindingMode.OneWay);
     source.SourceItems.Add("1");
     binding.Bind();
     Assert.IsTrue(target.Items[0] == "1");
     source.SourceItems.Remove("1");
     Assert.IsTrue(target.Items.Count == 0);
 }
示例#4
0
 public static void TestConversion()
 {
     SourceClass source = new SourceClass();
     TargetClass target = new TargetClass();
     BindingBase binding = new BindingBase(target, "TargetInt", source, "SourceStr", BindingMode.OneWay);
     BindingBase binding2 = new BindingBase(target, "TargetStr", source, "SourceInt", BindingMode.OneWay);
     binding.Bind();
     binding2.Bind();
     source.SourceInt = 5;
     source.SourceStr = "4";
     Assert.AreEqual(target.TargetInt, 4);
     Assert.AreEqual(target.TargetStr, "5");
 }
//        [TestMethod]
        public void TestMethod1()
        {
            SourceClass source = new SourceClass(  );
            TargetClass target = new TargetClass(  );
            BindingBase binding = new BindingBase( target, "TargetStr", source, "SourceInt" );
            BindingResult lastResult = null;
            binding.OnBinding += result => {
                lastResult = result;
            };
            binding.Bind(  );
            target.TargetStr = "5";
            Assert.IsTrue( source.SourceInt == 5 );
            target.TargetStr = "invalid int";
            Assert.IsTrue(source.SourceInt == 0);
            Assert.IsTrue( lastResult.hasConversionError );
        }
 public void TestMethod1()
 {
     TargetClass target = new TargetClass(  );
     SourceClass source = new SourceClass(  );
     BindingBase binding = new BindingBase( target, "Text", source, "Val" );
     binding.Converter = new DoubleToStringConverter(  );
     binding.Bind(  );
     source.Val = 3.0f;
     Assert.IsTrue( target.Text == "3" );
     target.Text = "0.5";
     Assert.IsTrue( source.Val == 0.5 );
 }
//        [TestMethod]
        public void TestValidation( ) {
            SourceClass source = new SourceClass();
            TargetClass target = new TargetClass();
            BindingBase binding = new BindingBase(target, "TargetStr", source, "SourceInt", BindingMode.OneWay);
            binding.UpdateSourceIfBindingFails = false;
            binding.Bind();
            target.TargetInt = 1;
            source.SourceStr = "invalid int";
            Assert.IsTrue( target.TargetInt == 1 );
        }
示例#8
0
//        [TestMethod]
        public void TestMethod1()
        {
            SourceClass source = new SourceClass(  );
            TargetClass target = new TargetClass(  );
            BindingBase binding = new BindingBase( target, "S", source, "Str" );
            binding.Adapter = new MyAdapter(  );
            binding.Bind();
            source.Str = "123";
            Assert.IsTrue( target.GetTargetStr(  ) == "123" );
            target.SetTargetStr( "456" );
            Assert.IsTrue(source.Str == "456");
            binding.Unbind(  );
            source.Str = "123";
            Assert.IsTrue(target.GetTargetStr() == "456");
        }