public Window() { this.WindowStartUpLocation = WindowStartUpLocation.CenterScreen; rcBackground = new System.Windows.Shapes.Rectangle(); rcBackground.StrokeThickness = 1; rcBackground.Parent = this; rcBackground.RadiusX = 0; rcBackground.RadiusY = 0; rcBackground.SetBinding ( System.Windows.Shapes.Rectangle.FillProperty, new Binding("Background") { Source = this, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); rcBackground.SetBinding ( System.Windows.Shapes.Rectangle.StrokeProperty, new Binding("BorderBrush") { Source = this, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, }); }
public SpriteElement() { m_transform = new TranslateTransform(); m_brush = new ImageBrush() { Stretch = Stretch.None, AlignmentX = AlignmentX.Left, AlignmentY = AlignmentY.Top, Transform = m_transform }; var rect = new Rectangle() { Fill = m_brush }; rect.SetBinding(Rectangle.WidthProperty, new Binding("SpriteWidth") { Source = this }); rect.SetBinding(Rectangle.HeightProperty, new Binding("SpriteHeight") { Source = this }); this.Content = rect; }
/// <summary> /// Creates the window command rectangle. /// </summary> /// <param name="parentButton">The parent button.</param> /// <param name="style">The style.</param> /// <returns>Rectangle.</returns> public static Rectangle CreateWindowCommandRectangle(Button parentButton, string style) { Argument.IsNotNull(() => parentButton); Argument.IsNotNullOrWhitespace(() => style); var rectangle = new Rectangle { Width = 16d, Height = 16d, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Stretch = Stretch.UniformToFill }; rectangle.SetBinding(Rectangle.FillProperty, new Binding("Foreground") { Source = parentButton }); var application = Application.Current; if (application != null) { rectangle.OpacityMask = new VisualBrush { //Stretch = Stretch.Fill, Visual = application.FindResource(style) as Visual }; } return rectangle; }
public static void BindParameterToOpacityMaskImageSourceWithConverter(int i, string prop, System.Windows.Shapes.Rectangle img, string definition, IValueConverter conv) { var b = new Binding { Source = Data.CharList[i], Path = new PropertyPath(prop), UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, Converter = conv, ConverterParameter = definition }; img.SetBinding(Shape.OpacityMaskProperty, b); }
private IEnumerable<UIElement> CreateWorkspaceBackground() { var back = new Rectangle(); back.Fill = Brushes.Wheat; back.AllowDrop = true; back.Drop += workSpace_Drop_1; var widthBinding = new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Canvas), 1), Path = new PropertyPath("ActualWidth") }; var heightBinding = new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Canvas), 1), Path = new PropertyPath("ActualHeight") }; back.SetBinding(Rectangle.WidthProperty, widthBinding); back.SetBinding(Rectangle.HeightProperty, heightBinding); yield return back; }
private Rectangle createRectangle(System.Windows.VerticalAlignment verticalAlignment, Brush color) { Rectangle r = new Rectangle(); Binding widthBinding = new Binding("Width"); widthBinding.Source = this; r = new Rectangle(); r.DataContext = this; r.VerticalAlignment = verticalAlignment; r.Fill = color; r.SetBinding(Rectangle.WidthProperty, widthBinding); return r; }
/// <inheritdoc/> protected override UIElement OnGetLegendSymbol() { var grid = new Grid { MinWidth = 16, MinHeight = 16, }; var background = new Rectangle { Width = 16, Height = 16, }; background.SetBinding(Shape.FillProperty, new Binding("Background") { Source = this }); grid.Children.Add(background); return grid; }
private void InitializeGrid() { Trace.WriteLine("Initializing Grid"); int numRows = this.ViewModel.GridWidth; int numCols = this.ViewModel.GridHeight; double cellWidth = this.gameOfLifeCanvas.Width / numCols; double cellHeight = this.gameOfLifeCanvas.Height / numRows; CellStateToBrushConverter cellStateToBrushConverter = new CellStateToBrushConverter(); for (int columnIndex = 0; columnIndex < numCols; columnIndex++) { for (int rowIndex = 0; rowIndex < numRows; rowIndex++) { Rectangle cellRect = new Rectangle { Width = cellWidth, Height = cellHeight, RenderTransform = new TranslateTransform(columnIndex * cellWidth, rowIndex * cellHeight), RadiusX = cellWidth / 10, RadiusY = cellHeight / 10 }; CellContainer cellContainer = new CellContainer(this.ViewModel[rowIndex, columnIndex]); cellRect.InputBindings.Add(new MouseBinding(this.ViewModel.CellClickedCommand, new MouseGesture(MouseAction.LeftClick)) { CommandParameter = cellContainer }); cellRect.SetBinding(Shape.FillProperty, new Binding("State") { Source = cellContainer, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.Explicit, Converter = cellStateToBrushConverter }); this.gameOfLifeCanvas.Children.Add(cellRect); } } Trace.WriteLine("Grid Initialized"); }
private void build_gui() { this.DataContext = spectrum; Rectangle rect1 = new Rectangle(); rect1.Width = 70; rect1.Height = 70; Binding bind_brush = new Binding("SpectrumBrush"); rect1.SetBinding(Rectangle.FillProperty, bind_brush); StackPanel sp = new StackPanel(); StackPanel sp1 = this.tb_slider("R", "Red:"); StackPanel sp2 = this.tb_slider("G", "Green:"); StackPanel sp3 = this.tb_slider("B", "Blue:"); sp.Children.Add(sp1); sp.Children.Add(sp2); sp.Children.Add(sp3); Slider rainbow_slider = new Slider(); rainbow_slider.Width = 300; rainbow_slider.Minimum = 0.0; rainbow_slider.Maximum = 1.0; rainbow_slider.Background = this.spectrum.rainbow_brush(new Point(0.0, 0.5), new Point(1.0, 0.5)); Binding bind_rainbow = new Binding("RainbowValue"); rainbow_slider.SetBinding(Slider.ValueProperty, bind_rainbow); StackPanel sp_color = new StackPanel(); sp_color.Orientation = Orientation.Horizontal; sp_color.Height = 80; sp_color.Children.Add(rect1); sp_color.Children.Add(sp); StackPanel all = new StackPanel(); all.Children.Add(sp_color); all.Children.Add(rainbow_slider); all.Height = 120; all.Width = 300; this.Content = all; }
public override void OnApplyTemplate() { base.OnApplyTemplate(); Overlay = GetTemplateChild("Overlay") as Rectangle; if (Overlay != null) { // Tie the IsEnabled property to the visibility of the overlay rectangle (to give it a grayed out effect) Binding binding = new Binding() { Source = this, Path = new PropertyPath("IsEnabled"), Converter = new ReverseVisibilityConverter(), }; Overlay.SetBinding(Rectangle.VisibilityProperty, binding); } SymbolsList = GetTemplateChild("SymbolsList") as ItemsControl; if (SymbolsList != null) { SymbolsList.MouseLeftButtonUp += new MouseButtonEventHandler(Symbols_MouseLeftButtonUp); } }
public override void OnApplyTemplate() { base.OnApplyTemplate(); SampleSelector = GetTemplateChild(SampleSelectorName) as Grid; SelectedHueColor = GetTemplateChild(SelectedHueColorName) as Rectangle; var body = GetTemplateChild(BodyName) as Grid; if (body != null) { _monitor = new MovementMonitor(); _monitor.Movement += _monitor_Movement; _monitor.MonitorControl(body); } ColorSlider = GetTemplateChild(ColorSliderName) as ColorSlider; if (ColorSlider != null) { if (Thumb == null) Thumb = new ColorSliderThumb(); ColorSlider.ColorChanged += ColorSlider_ColorChanged; if(SelectedHueColor != null) { var binding = new System.Windows.Data.Binding { Source = ColorSlider, Path = new PropertyPath("SolidColorBrush"), }; SelectedHueColor.SetBinding(Shape.FillProperty, binding); } } }
private void AddFakeComponent(FrameworkElement control, object item) { if (control != null && item != null) { var fakeControl = new Rectangle(); fakeControl.Tag = item; fakeControl.Fill = Brushes.Transparent; //InkCanvas.SetTop(fakeControl, item.Position.X); //InkCanvas.SetLeft(fakeControl, item.Position.Y); fakeControl.Width = control.ActualWidth; fakeControl.Height = control.ActualHeight; var positionBindingPrefix = ""; if (typeof (DraggableComponent).IsAssignableFrom(item.GetType())) { positionBindingPrefix = "Position."; } var leftBinding = new Binding(positionBindingPrefix + "X"); leftBinding.Mode = BindingMode.TwoWay; leftBinding.Source = item; fakeControl.SetBinding(InkCanvas.LeftProperty, leftBinding); var rightBinding = new Binding(positionBindingPrefix + "Y"); rightBinding.Mode = BindingMode.TwoWay; rightBinding.Source = item; fakeControl.SetBinding(InkCanvas.TopProperty, rightBinding); if (item.GetType() == typeof (TextFrame)) { var widthBinding = new Binding("Width"); widthBinding.Mode = BindingMode.TwoWay; widthBinding.Source = item; fakeControl.SetBinding(WidthProperty, widthBinding); var heightBinding = new Binding("Height"); heightBinding.Mode = BindingMode.TwoWay; heightBinding.Source = item; fakeControl.SetBinding(HeightProperty, heightBinding); } else { fakeControl.MinHeight = fakeControl.Height; fakeControl.MinWidth = fakeControl.Width; fakeControl.MaxHeight = fakeControl.Height; fakeControl.MaxWidth = fakeControl.Width; } InkLayer.Children.Add(fakeControl); } }
public void ElementName_BeforeAddToTree_2 () { var source = new Rectangle { Name = "Source", Width = 100, }; var target = new Rectangle { Name = "Target" }; target.SetBinding (Rectangle.WidthProperty, new Binding { ElementName = "Source", Path = new PropertyPath ("Width"), Mode = BindingMode.OneWay, }); CreateAsyncTest (target, () => { TestPanel.Children.Add (source); }, () => { Assert.IsTrue (double.IsNaN (target.Width), "#1"); } ); }
public void ElementName_AfterAddToTree () { var source = new Rectangle { Name = "Source", Width = 100, }; var target = new Rectangle { Name = "Target" }; target.SetBinding (Rectangle.WidthProperty, new Binding { ElementName = "Source", Path = new PropertyPath ("Width"), Mode = BindingMode.OneWay, }); TestPanel.Children.Add (source); CreateAsyncTest (target, () => { Assert.AreEqual (100, target.Width, "#2"); }); }
public void BindInheritedClass () { InheritedData data = new InheritedData (); data.InnerData = new Data { Opacity = 1.0f }; Rectangle rectangle = new Rectangle { Opacity = 0f }; rectangle.DataContext = data; Binding binding = new Binding ("InnerData.Opacity"); rectangle.SetBinding (Shape.OpacityProperty, binding); Assert.AreEqual (data.InnerData.Opacity, rectangle.Opacity, "#1"); binding = new Binding ("Float"); rectangle.SetBinding(Shape.OpacityProperty, binding); }
public void ChangeSourceValue() { Data data = new Data { Opacity = 0.5 }; Rectangle r = new Rectangle(); r.SetBinding(Rectangle.OpacityProperty, new Binding { Path = new PropertyPath("Opacity"), Source = data }); Assert.AreEqual(data.Opacity, r.Opacity, "#1"); data.Opacity = 0; Assert.AreNotEqual(data.Opacity, r.Opacity, "#2"); }
/// <summary> /// Creates a color preview control. /// </summary> /// <param name="d"> /// The definition. /// </param> /// <param name="index"> /// The index. /// </param> /// <returns> /// A preview control. /// </returns> protected virtual FrameworkElement CreateColorPreviewControl(PropertyDefinition d, int index) { var c = new Rectangle { Stroke = Brushes.Black, StrokeThickness = 1, Width = 12, Height = 12, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center }; var binding = d.CreateBinding(index); binding.Converter = new ColorToBrushConverter(); c.SetBinding(Shape.FillProperty, binding); return c; }
public void BindToDP_WrongDPName_WithWrongName () { var data = new UnbackedDPs (); var rect = new Rectangle (); rect.SetBinding (Rectangle.WidthProperty, new Binding { Path = new PropertyPath ("WrongPropertyName"), Source = data, }); Assert.IsTrue(Double.IsNaN (rect.Width), "#1"); }
public void BasicBind () { Rectangle rectangle = new Rectangle (); Binding binding = new Binding ("Opacity"); binding.Source = new Data { Opacity = 0.0 }; rectangle.SetBinding (Rectangle.OpacityProperty, binding); Assert.AreEqual (0.0, rectangle.Opacity, "#1"); Assert.IsTrue (rectangle.ReadLocalValue (Rectangle.OpacityProperty) is BindingExpressionBase, "#2"); rectangle.Opacity = 1.0; Assert.AreEqual(1.0, rectangle.Opacity, "#3"); Assert.AreEqual (1.0, rectangle.ReadLocalValue (Rectangle.OpacityProperty), "#4"); rectangle.SetBinding (Rectangle.OpacityProperty, binding); Assert.IsTrue (rectangle.ReadLocalValue (Rectangle.OpacityProperty) is BindingExpressionBase, "#5"); Assert.AreEqual (0.0, rectangle.Opacity, "#6"); rectangle.ClearValue (Rectangle.OpacityProperty); Assert.AreEqual (1.0, rectangle.Opacity, "#7"); Assert.AreEqual (DependencyProperty.UnsetValue, rectangle.ReadLocalValue (Rectangle.OpacityProperty), "#8"); }
public void BindFloatToDouble () { Binding binding = new Binding ("Opacity"); binding.Source = new OpacityTest { Opacity = 0.5f }; Rectangle r = new Rectangle (); r.SetBinding (Rectangle.OpacityProperty, binding); Assert.IsBetween (0.499, 0.5001, r.Opacity, "#1"); }
public void DataContext_ClearLocal () { Binding b = new Binding(""); Canvas c = new Canvas(); c.DataContext = new SolidColorBrush(Colors.Blue); Rectangle r = new Rectangle(); r.DataContext = new SolidColorBrush(Colors.Green); c.Children.Add(r); r.SetBinding(Rectangle.FillProperty, b); // clearing the value allows the inherited // DataContext to be used again (and causes // the bound property to be updated) r.ClearValue (FrameworkElement.DataContextProperty); Assert.AreEqual(c.DataContext, r.DataContext, "#1"); Assert.AreEqual(c.DataContext, r.Fill, "#2"); }
public void DataContext_SetBindingSource () { Binding b = new Binding(""); Canvas c = new Canvas(); c.DataContext = new SolidColorBrush(Colors.Blue); Rectangle r = new Rectangle(); r.DataContext = new SolidColorBrush(Colors.Green); c.Children.Add(r); // set the source of the Binding object b = new Binding (""); b.Source = new SolidColorBrush (Colors.Yellow); r.SetBinding(Rectangle.FillProperty, b); Assert.AreEqual(b.Source, r.Fill, "#1"); // now set the data context, and show that the // Binding.Source has precedence. r.DataContext = new LinearGradientBrush(); Assert.AreEqual(b.Source, r.Fill, "#2"); }
public void BindToDP_WrongDPName_WithProperty () { var data = new UnbackedDPs (); var rect = new Rectangle (); var binding = new Binding (); binding.Source = data; Assert.Throws<Exception> (() => binding.Path = new PropertyPath (UnbackedDPs.WrongPropertyName) ); return; rect.SetBinding (Rectangle.WidthProperty, binding); Assert.AreEqual (5, rect.Width, "#1"); }
public void DataContext_NullLocal () { Binding b = new Binding(""); Canvas c = new Canvas(); c.DataContext = new SolidColorBrush(Colors.Blue); Rectangle r = new Rectangle(); r.DataContext = new SolidColorBrush(Colors.Green); c.Children.Add(r); r.SetBinding(Rectangle.FillProperty, b); // a null DataContext is still a local value, // and takes precedence over the inherited // value. r.DataContext = null; Assert.AreEqual(null, r.Fill, "#1"); }
public void DataContext_Precedence () { Binding b = new Binding(""); Canvas c = new Canvas(); c.DataContext = new SolidColorBrush(Colors.Blue); Rectangle r = new Rectangle(); r.DataContext = new SolidColorBrush(Colors.Green); c.Children.Add(r); r.SetBinding(Rectangle.FillProperty, b); // the local DataContext takes precedent. Assert.AreEqual(r.Fill, r.DataContext, "#1"); }
/// <summary> /// Handles the Loaded event of the Window control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private void Window_Loaded(object sender, RoutedEventArgs e) { newGameViewModel = new NewGameViewModel(canvas.ActualWidth, canvas.ActualHeight); optionsViewModel = new OptionsViewModel(); this.DataContext = newGameViewModel; newGameViewModel.PresetValues(); foreach (var item in newGameViewModel.BallList) { Ball ujlabda = item; Ellipse ujLabdaEllipse = new Ellipse(); ImageBrush myBrush = new ImageBrush(); myBrush.ImageSource = new BitmapImage(new Uri(item.ImagePath,UriKind.Relative)); ujLabdaEllipse.Fill = myBrush; canvas.Children.Add(ujLabdaEllipse); Binding widthBinding = new Binding("Area.Width"); widthBinding.Source = ujlabda; ujLabdaEllipse.SetBinding(Ellipse.WidthProperty, widthBinding); Binding heightBinding = new Binding("Area.Height"); heightBinding.Source = ujlabda; ujLabdaEllipse.SetBinding(Ellipse.HeightProperty, heightBinding); Binding xBinding = new Binding("Area.X"); xBinding.Source = ujlabda; ujLabdaEllipse.SetBinding(Canvas.LeftProperty, xBinding); Binding yBinding = new Binding("Area.Y"); yBinding.Source = ujlabda; ujLabdaEllipse.SetBinding(Canvas.TopProperty, yBinding); } foreach (var item in newGameViewModel.BrickList) { Brick ujbrick = item; Rectangle ujBrickRect = new Rectangle(); ImageBrush myBrush = new ImageBrush(); myBrush.ImageSource = new BitmapImage(new Uri(item.ImagePath, UriKind.Relative)); ujBrickRect.Fill = myBrush; Binding widthBinding = new Binding("Area.Height"); widthBinding.Source = ujbrick; ujBrickRect.SetBinding(Rectangle.WidthProperty, widthBinding); Binding heightBinding = new Binding("Area.Width"); heightBinding.Source = ujbrick; ujBrickRect.SetBinding(Rectangle.HeightProperty, heightBinding); Binding xBinding = new Binding("Area.X"); xBinding.Source = ujbrick; ujBrickRect.SetBinding(Canvas.LeftProperty, xBinding); Binding yBinding = new Binding("Area.Y"); yBinding.Source = ujbrick; ujBrickRect.SetBinding(Canvas.TopProperty, yBinding); canvas.Children.Add(ujBrickRect); } foreach (var item in newGameViewModel.RacketList) { Racket racket = item; Rectangle ujracket = new Rectangle(); ImageBrush myBrush = new ImageBrush(); myBrush.ImageSource = new BitmapImage(new Uri(item.ImagePath, UriKind.Relative)); ujracket.Fill = myBrush; Binding widthBinding = new Binding("Area.Width"); widthBinding.Source = racket; ujracket.SetBinding(Rectangle.WidthProperty, widthBinding); Binding heightBinding = new Binding("Area.Height"); heightBinding.Source = racket; ujracket.SetBinding(Rectangle.HeightProperty, heightBinding); Binding xBinding = new Binding("Area.X"); xBinding.Source = racket; ujracket.SetBinding(Canvas.LeftProperty, xBinding); Binding yBinding = new Binding("Area.Y"); yBinding.Source = racket; ujracket.SetBinding(Canvas.TopProperty, yBinding); canvas.Children.Add(ujracket); } }
public void BindPropertyWhichIsINotifyPropChanged () { // This tests which object we attach INotifyPropertyChanged handlers to. // 'data' is our source but 'data.Updater' is the object which has the // property we've databound to. In this case, we attach our INPC handlers // to 'data.Updater' var data = new ContainsPropertyUpdater (); Rectangle rectangle = new Rectangle (); rectangle.SetBinding (Rectangle.OpacityProperty, new Binding ("Updater.Opacity")); rectangle.DataContext = data; data.Updater.Opacity = 0.0f; Assert.AreEqual (0.0, rectangle.Opacity, "#1"); data.Updater.Opacity = 1; Assert.AreEqual (1, rectangle.Opacity, "#2"); }
public void AttachedProperty_Simple () { var data = new Rectangle (); Canvas.SetTop (data, 100); var target = new Rectangle (); target.SetBinding (Rectangle.WidthProperty, new Binding { Path = new PropertyPath ("(Canvas.Top)"), Source = data, }); Assert.AreEqual (100, target.Width, "#1"); }
public void BindInternalClass () { InternalData data = new InternalData (); data.InnerData = new Data { Opacity = 1.0f }; Rectangle rectangle = new Rectangle { Opacity = 0f }; rectangle.DataContext = data; Binding binding = new Binding ("InnerData.Opacity"); rectangle.SetBinding (Shape.OpacityProperty, binding); }
public void DataContext_ChangeParentOneWay () { Canvas canvas = new Canvas (); PropertyUpdater updater = new PropertyUpdater { Opacity = 0 }; Binding binding = new Binding ("Opacity"); Rectangle rectangle = new Rectangle { Name = "TED" }; canvas.DataContext = updater; canvas.Children.Add (rectangle); rectangle.SetBinding (Rectangle.OpacityProperty, binding); Assert.AreSame (rectangle.DataContext, canvas.DataContext, "#1"); updater.Opacity = 0; Assert.AreEqual (0, rectangle.Opacity, "#2"); canvas.DataContext = null; Assert.AreEqual (1, rectangle.Opacity, "#3"); updater.Opacity = 0.5f; Assert.AreEqual (1, rectangle.Opacity, "#4"); }
public void BindToDP_WrongDPName_WithName () { var data = new UnbackedDPs (); var rect = new Rectangle (); rect.SetBinding (Rectangle.WidthProperty, new Binding { Path = new PropertyPath ("SomeDP"), Source = data, }); Assert.AreEqual (5, rect.Width, "#1"); }