A converter return a border. It doesn't work.
-
Tuesday, August 21, 2012 11:09 PM
When the tag of b1 changes, I want to change the child of the b1.
the converter of returnBorder is returning a new border.
<Border Name="b1" Tag="HelloWorld"> <Border.Child> <Binding ElementName="b1" Path="Tag" Converter="{StaticResource returnBorder}"></Binding> </Border.Child> </Border>
- Edited by yqhszn20j Tuesday, August 21, 2012 11:11 PM
All Replies
-
Tuesday, August 21, 2012 11:58 PMModerator
Border.Child (or rather Decorator.Child) is not a dependency property, so you can't bind to it.
You can however, use your own attached property, to set the Child property "from within", so to speak.
using System.Windows; using System.Windows.Controls; namespace WpfApplication68.Helpers { class Attached { public static UIElement GetBindableChild(DependencyObject obj) { return (UIElement)obj.GetValue(BindableChildProperty); } public static void SetBindableChild(DependencyObject obj, UIElement value) { obj.SetValue(BindableChildProperty, value); } public static readonly DependencyProperty BindableChildProperty = DependencyProperty.RegisterAttached("BindableChild", typeof(UIElement), typeof(Attached), new UIPropertyMetadata(null, BindableChildPropertyChanged)); static void BindableChildPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var ele = sender as Decorator; ele.Child = (UIElement)e.NewValue; } } }
using System; using System.Windows.Data; using System.Windows.Controls; using System.Windows.Media; namespace WpfApplication68.Helpers { public class MyConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) return null; var border = new Border { Background = Brushes.Red, Width=100, Height=50 }; border.Child = new TextBlock { Text = value.ToString() }; return border; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
<Window x:Class="WpfApplication68.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:WpfApplication68.Helpers" > <Window.Resources> <local:MyConverter x:Key="MyConverter"/> </Window.Resources> <Grid> <Border local:Attached.BindableChild="{Binding Tag, RelativeSource={RelativeSource Self}, Converter={StaticResource MyConverter}}" Tag="Hello World" /> </Grid> </Window>BTW, I uploaded the demo to MSDN Samples, in case you cannot replicate this from the code snippets above:
Binding to Border Child (aka Decorator Child)
(please rate it if you like it ;)
Regards,
Pete
#PEJL
- Edited by XAML guyMicrosoft Community Contributor, Moderator Tuesday, August 21, 2012 11:58 PM tweaks
- Edited by XAML guyMicrosoft Community Contributor, Moderator Wednesday, August 22, 2012 12:28 AM added msdn samples link
- Edited by XAML guyMicrosoft Community Contributor, Moderator Wednesday, August 22, 2012 12:29 AM
- Marked As Answer by Annabella LuoModerator Wednesday, August 29, 2012 8:41 AM

