я создал фиктивное окно с{Binding ELEMENTNAME ...} неспособность
- флажком
- кнопкой
- кнопка внутри пользовательского UserControl
Обе кнопки запуска из Синий, и когда флажок установлен, они оба должны стать оранжевыми. Тем не менее, внутри пользовательского элемента управления, по-видимому, игнорируется флажок. Почему это не удается, и как я могу обойти это?
UserControl Я хочу, чтобы вставить:
<UserControl x:Class="UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
<Grid>
<!--MyContent is a dependency property in UserControl1's code behind-->
<ContentControl Content="{Binding MyContent}">
</Grid>
</UserControl>
Главное окно:
<Window x:Class="WpfApplication11.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">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="Button">
<Setter Property="Background" Value="Cyan"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=myCheckbox, Path=isChecked}" Value="True">
<Setter Property="Background" Value="Orange/>
</DataTrigger>
</Style.Triggers>
</Style>
<Button x:Key="Button1" Content="Button1"/>
<Button x:Key="Button2" Content="Button2"/>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<CheckBox x:Name = "myCheckBox" Content="Turn Orange" Grid.Row="0"/>
<!--This updates when I check myCheckBox-->
<ContentControl Content = "{StaticResource Button1}" Grid.Row="2"/>
<!--This does NOT update when I check myCheckBox-->
<test:UserControl1 MyContent="{StaticResource Button2}" Grid.Row="2"/>
</Grid>
</Window>
Невозможно это гарантировать; но у меня есть сильное чувство, что 'myCheckbox' выходит за рамки, когда вы находитесь внутри' UserControl1'. Я предполагаю, что у вас есть Исключение System.Data. Не так много вы можете сделать по этому поводу, но не полагайтесь на elementName так много – BradleyDotNET