に失敗青、チェックボックスをオンにすると、両方ともオレンジ色に変わります。ただし、ユーザーコントロール内のチェックボックスを無視しているようです。 なぜそれが失敗し、どうすればこの問題を回避できますか?{がelementNameバインディング...}私は両方のボタンが出て起動し、カスタムユーザーコントロール内のボタン</li> <li>ボタン</p> <ul> <li>で</li> </ul> <p>をチェックボックス</li> <li>をダミーのウィンドウを作成した
私が挿入したいユーザーコントロール:
<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>
メインウィンドウ:あなたはスタイルからmyCheckBoxを参照することはできません
<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>
保証できません。私はあなたが 'UserControl1'の中にいたら、' myCheckbox'が範囲外になっているという強い思いがあります。私はあなたに、その効果に対するSystem.Data例外があると推測しています。それほど多くはありませんが、elementNameに頼らないでください。 – BradleyDotNET