2017-01-19 12 views
0

に失敗青、チェックボックスをオンにすると、両方ともオレンジ色に変わります。ただし、ユーザーコントロール内のチェックボックスを無視しているようです。 なぜそれが失敗し、どうすればこの問題を回避できますか?{が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> 
+0

保証できません。私はあなたが 'UserControl1'の中にいたら、' myCheckbox'が範囲外になっているという強い思いがあります。私はあなたに、その効果に対するSystem.Data例外があると推測しています。それほど多くはありませんが、elementNameに頼らないでください。 – BradleyDotNET

答えて

1

ユーザーコントロールに "myCheckBox" という名前のチェックボックスが存在しないので、なぜそれが

を失敗しています。親ウィンドウ内のCheckBoxは別の命名スコープ内にあり、ElementNameへのバインディングはここでは機能しません。

どうすればこの問題を回避できますか?

MVVMデザインパターンを学習して実装する:https://msdn.microsoft.com/en-us/library/hh848246.aspx XAMLベースのUIアプリケーションを開発する際に推奨するデザインパターンです。

のCheckBoxの現在の状態を保持しているビューモデルクラスを作成します。

public class ViewModel : INotifyPropertyChanged 
{ 
    private bool _isChecked; 
    public bool IsChecked 
    { 
     get { return _isChecked; } 
     set { _isChecked = value; OnPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

をそして、これのインスタンスに、ウィンドウのDataContextのを設定し、代わりにチェックボックスに直接結合する、そのにisCheckedプロパティにバインドします。

<Window ...> 
    <Window.Resources> 
     <Style TargetType="Button"> 
      <Setter Property="Background" Value="Cyan"/> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsChecked}" Value="True"> 
        <Setter Property="Background" Value="Orange"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
     <Button x:Key="Button1" Content="Button1"/> 
     <Button x:Key="Button2" Content="Button2"/> 
    </Window.Resources> 
    <Window.DataContext> 
     <test:ViewModel /> 
    </Window.DataContext> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
      <RowDefinition Height="1*"/> 
     </Grid.RowDefinitions> 

     <CheckBox x:Name ="myCheckBox" Content="Turn Orange" Grid.Row="0" IsChecked="{Binding IsChecked}"/> 

     <ContentControl Content = "{StaticResource Button1}" Grid.Row="1"/> 

     <test:UserControl2 MyContent="{StaticResource Button2}" Grid.Row="2"/> 

    </Grid> 
</Window> 

ユーザーコントロールはとてもUserControl1.xamlで明示的にDataContextプロパティを設定していない親ウィンドウのDataContextのを継承する必要があります。

<UserControl x:Class="WpfApplication11.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" 
      xmlns:local="clr-namespace:WpfApplication2" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <Grid> 
     <ContentControl Content="{Binding MyContent, RelativeSource={RelativeSource AncestorType=UserControl}}" /> 
    </Grid> 
</UserControl> 
+0

参考にしてください。http://stackoverflow.com/help/privileges/voteup – mm8

0

、これらは二つの異なるスコープがあります。さらに、スタイルはボタンを対象としていますが、コントロールはウィンドウです。スタイル内のすべてのコントロールをコントロールテンプレートに配置する必要があります。

関連する問題