2016-07-25 3 views
0

こんにちは私はこの2つの異なるモードを親のプロパティからバインディング可視性に設定します。任意の親のバインディング可視性親

<Window.Resources> 
    <BooleanToVisibilityConverter x:Key="BoolToVis"/> 
</Window.Resources> 
<Button x:Name="buttonTest" Width="75" Visibility="{Binding IsModify, Converter={StaticResource BoolToVis}}"/> 

が、視認性の結合に私のUserControlに私はこのようにして、それ以外の作品はありません:私がする必要があるなぜ私の質問は

<local:UserControl4 Height="100" Width="100" Visibility="{Binding ElementName=Window1,Path=IsModify,Converter={StaticResource BoolToVis}}"/> 

ですstandar制御用 は、この可能トム使用することですコントロールの可視性で要素名とParentPropertyを渡しますが、ボタンはありませんか?

これは、分離コード

public static readonly DependencyProperty IsModifyProperty = 
     DependencyProperty.Register("IsModify", typeof(Boolean), typeof(MainWindow), new PropertyMetadata(false)); 

    [DefaultValue(false)] 
    public Boolean IsModify 
    { 
     get { return (Boolean)GetValue(MainWindow.IsModifyProperty); } 
     set 
     { 
      SetValue(MainWindow.IsModifyProperty, value); 
      this.OnPropertyChanged("IsModify"); 
     } 
    } 

であり、これはここでエラーを複製するためのソースコードコンストラクタ

public MainWindow() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 

です。 1つのウィンドウと1つのusercontrolを持つ1つのプロジェクトが必要です。

窓用

ソースコード---- ---- XAML

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:System="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:XTesting" x:Name="windowTest" x:Class="XTesting.WindowTest" 
    Title="WindowTest" Height="300" Width="583.908"> 

<Window.Resources> 
    <ResourceDictionary> 
     <BooleanToVisibilityConverter x:Key="BoolToVis"/> 
    </ResourceDictionary> 
</Window.Resources> 

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition/> 
    </Grid.RowDefinitions> 

    <local:UserControl4 
     HorizontalAlignment="Left" 
     Height="65" 
     Margin="300,30,0,0" 
     VerticalAlignment="Top" 
     Width="230" 
     Visibility="{Binding IsModify, Converter={StaticResource BoolToVis}}" 
     Background="#FFF86161" Description="Custom Control"/> 

    <Button x:Name="buttonTest" 
      Content="Modify" 
      HorizontalAlignment="Left" 
      Margin="45,30,0,0" 
      VerticalAlignment="Top" 
      Width="225" 
      Visibility="{Binding IsModify, Converter={StaticResource BoolToVis}}" 
      Height="65"/> 

    <Button x:Name="button" 
      Content="Skisem (Click me)" 
      HorizontalAlignment="Left" 
      Height="75" 
      Margin="225,160,0,0" 
      VerticalAlignment="Top" 
      Width="135" 
      Click="button_Click_1" 
      Cursor="Hand"/> 
</Grid> 

これは、これがユーザーコントロール のソースコードであるウィンドウの分離コード

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
namespace XTesting 
{ 
/// <summary> 
/// Interaction logic for WindowTest.xaml 
/// </summary> 
public partial class WindowTest : Window 
{ 
    public static readonly DependencyProperty IsModifyProperty = 
    DependencyProperty.Register("IsModify", typeof(Boolean), typeof(WindowTest), new PropertyMetadata(false)); 
    [DefaultValue(false)] 
    public Boolean IsModify 
    { 
     get { return (Boolean)GetValue(WindowTest.IsModifyProperty); } 
     set 
     { 
      SetValue(WindowTest.IsModifyProperty, value); 
      this.OnPropertyChanged("IsModify"); 
     } 
    } 

    public WindowTest() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 

    private void button_Click_1(object sender, RoutedEventArgs e) 
    { 
     IsModify = !IsModify; 
    } 

    #region - INotifyPropertyChanged implementation - 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion - INotifyPropertyChanged implementation - 
} 
} 

ありますXAML:

<UserControl x:Class="XTesting.UserControl4" 
     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="37" d:DesignWidth="310" MinWidth="7" 
     MouseLeftButtonUp="Selector1_MouseLeftButtonUp" FontSize="20"> 

<UserControl.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries>    
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</UserControl.Resources> 


<Grid x:Name="GridRoot"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="37*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="51"/> 
     <ColumnDefinition Width="259*"/> 
    </Grid.ColumnDefinitions> 

    <TextBox x:Name="TextBoxDescription" 
      Margin="0" 
      TextWrapping="Wrap" 
      Grid.Column="1" 
      Text="{Binding Description}" 
      Background="{x:Null}" 
      BorderBrush="{x:Null}" 
      Foreground="{Binding Foreground,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
      FontSize="{Binding FontSize,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" 
      VerticalContentAlignment="Center" 
      IsReadOnly="True" 
      SelectionBrush="{x:Null}" 
      BorderThickness="0" 
      Focusable="False" 
      IsTabStop="False" 
      IsUndoEnabled="False" 
      AllowDrop="False" 
      Padding="1,0,0,0" 
      MaxLines="1" 
      /> 
</Grid> 

と、これは背後にあるコードです:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
namespace XTesting 
{ 
/// <summary> 
/// Interaction logic for UserControl4.xaml 
/// </summary> 
public partial class UserControl4 : UserControl, INotifyPropertyChanged 
{ 
    #region - Delegate - 
    public delegate void ClickHandler(Object sender, RoutedEventArgs e);  
    #endregion - Delegate - 
    #region - Events - 
    public event ClickHandler Click;  
    #endregion - Events - 

    #region - Dependency Properties mandatory for Binding - 
    public static readonly DependencyProperty DescriptionProperty = 
     DependencyProperty.Register("Description", typeof(String), typeof(UserControl4), new PropertyMetadata(String.Empty));  
    #endregion - Dependency Properties for Binding - 

    #region - Properties -  
    /// <summary> 
    /// Gets or sets the description 
    /// </summary> 
    [Category("Common"), Description("gets or sets The description")] 
    public String Description 
    { 
     get { return (String)GetValue(UserControl4.DescriptionProperty); } 
     set 
     { 
      SetValue(UserControl4.DescriptionProperty, value); 
      this.OnPropertyChanged("Description"); 
     } 
    } 
    #endregion - Properties - 

    public UserControl4() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 
    private void Selector1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     if (Click != null) Click(sender, e);   
    } 

    #region - INotifyPropertyChanged implementation - 
    // Basically, the UI thread subscribes to this event and update the binding if the received Property Name correspond to the Binding Path element 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    #endregion - INotifyPropertyChanged implementation - 

} 
} 

私は問題はコンストラクタであると思いThis.datacontext =この; しかし、これはプロパティの説明をバインドする場合は必須です。事前

+0

なく、あなたの問題を再現することが可能。完全なコードを投稿してください。 –

+0

ok私はあなたが複製する必要があるすべてを追加します – luka

答えて

1

おかげであなたはおそらくGridRoot要素の代わりに、UserControl4クラス自体にユーザーコントロールDataContextを追加する必要があります。ユーザーコントロールの使用は、主にインスタンス化コードによって制御され、その内容のみがユーザーコントロール自体によって制御される必要があります。

はまた、私の代わりに、コンストラクタのLoadedイベントのデータコンテキストを割り当てることを好むが、これは私の個人的な好みかもしれません。

<UserControl x:Class="XTesting.UserControl4" Loaded="UserControl4_Loaded" ... 

// ... 

private void UserControl4_Loaded(object sender, RoutedEventArgs e) 
{ 
    GridRoot.DataContext = this; 
} 
+0

はい、ありがとう、今すぐ動作します。 – luka

+0

@lukaだから、もしこれがうまくいくなら、答えを受け入れるの? – grek40

+0

完了私は受け入れた! – luka

関連する問題