Я завел следующий конвертер:
class TextToVisualConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var window = values[0] as Window;
var elName = values[1] as string;
return window.FindName(elName);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Он получает 2 значения - родительское окно и имя элемента, ищет этот элемент в окне и возвращает его.
Тестовая разметка:
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="MainWindow" Width="400" Height="300"
Name="w">
<Window.Resources>
<local:TextToVisualConverter x:Key="conv"/>
</Window.Resources>
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="20"/>
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Name="txt" Text="b1"/>
<Button Name="b1" Grid.Row="1"
Content="Button 1" BorderBrush="Green"/>
<Button Name="b2" Grid.Row="2"
Content="Button 2" Background="Yellow"/>
<Rectangle Grid.Row="3">
<Rectangle.Fill>
<VisualBrush>
<VisualBrush.Visual>
<MultiBinding Converter="{StaticResource conv}">
<Binding ElementName="w"/>
<Binding ElementName="txt" Path="Text"/>
</MultiBinding>
</VisualBrush.Visual>
</VisualBrush>
</Rectangle.Fill>
</Rectangle>
</Grid>
</Window>
Пример работы: