2017-02-08 5 views
-1

WPFアプリケーションでは、4つのテキストボックスで構成されるユーザーコントロールがあります。このようなユーザコントロールの6つのユニットは、メインウィンドウのTabControlのタブアイテムにデータを取り込むためのデータテンプレートを形成します。データ層へのviewmodelを介してバインドされたデータテンプレートの内容。 私の問題は、ビュー(メインウィンドウ)から個々のTextBoxのIsEnabledプロパティにアクセスする方法が見つからないということです。各タブ項目の内容であるviewmodelはVisualではないため、VisualTreeHelperを使用するとTextBoxを正確にローカライズすることはできません。したがって、最大で1つのTextBoxインスタンスへの参照を取得しますが、すべてのタブで同時に動作します。 誰もがこれで私を助けることができますか?タブアイテムのコンテンツであるviewmodel内のWPFユーザーコントロールの要素にアクセスする方法は?

EDIT:

UserControl x:Class="WpfApplication4.MyModuleFrame" 
    <!-- ... --> 
    x:Name="mUserControl"> 
    <Grid> 
    <!-- ... --> 
    <TextBox Text="{Binding ItemSource.Ch1, ElementName=mUserControl}" Name="txtCh1"/> 
    <!-- other textboxes --> 
    </Grid> 
</UserControl> 

ユーザーコントロールのコードビハインド:

namespace WpfApplication4 
{ 
    public partial class MyModuleFrame : UserControl 
    { 
    public MyModuleFrame() 
    { 
     InitializeComponent(); 
    } 

    public Module ItemSource 
    { 
     get { return (Module)GetValue(ItemSourceProperty); } 
     set { SetValue(ItemSourceProperty, value); } 
    } 
     // Using a DependencyProperty 
     public static readonly DependencyProperty ItemSourceProperty = 
     DependencyProperty.Register("ItemSource", typeof(Module), typeof(MyModuleFrame), new PropertyMetadata(default(Module))); 
    } 
} 

Module.cs:

よりよいのための単純化されたコードスニペット以下 は私の問題

UserControlのXAMLを理解します

namespace WpfApplication4 
{ 
    public class Module: INotifyPropertyChanged 
    { 

    private double _ch1; 
    public double Ch1 
    { 
     get { return this._ch1; } 
     set 
     { 
      if (_ch1 == value) return; 
      _ch1 = value; 
      OnPropertyChanged("Ch1"); 
     } 
    } 
    ... 
    ... 
    ... 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string aNameOfProperty) 
    { 
     if (PropertyChanged != null) 
     PropertyChanged(this, new PropertyChangedEventArgs(aNameOfProperty)); 
    } 

ViewModel.cs:

namespace WpfApplication4 
{ 
public class MainViewModel: INotifyPropertyChanged 
{ 
    public Module Module1 { get; set; } 
    public Module Module2 { get; set; } 
    public Module Module3 { get; set; } 
    public Module Module4 { get; set; } 
    public Module Module5 { get; set; } 
    public Module Module6 { get; set; } 

    public MainViewModel() 
    { 
     Module1 = new Module() { Number = "1", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
     Module2 = new Module() { Number = "2", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
     Module3 = new Module() { Number = "3", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
     Module4 = new Module() { Number = "4", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
     Module5 = new Module() { Number = "5", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
     Module6 = new Module() { Number = "6", Ch1 = 123.4, Ch2 = 123.4, Ch3 = 123.4, Ch4 = 123.4 }; 
    } 
...   
    public event PropertyChangedEventHandler PropertyChanged; 
    protected virtual void RaisePropertyChanged(string propertyName) 
    { 
     this.VerifyPropertyName(propertyName); 
     PropertyChangedEventHandler handler = this.PropertyChanged; 
     if (handler != null) 
     { 
      var e = new PropertyChangedEventArgs(propertyName); 
      handler(this, e); 
     } 
    } 

メインウィンドウのXAML:

<Window x:Class="WpfApplication4.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    <!-- ... --> 
    Title="Window1" Height="800" Width="1000" x:Name="wndMain" 
    DataContext = "{StaticResource MainViewModel}"> 
<Window.Resources> 
    <DataTemplate x:Key="tabTemplate"> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="1*" /> 
       <ColumnDefinition Width="1*" /> 
       <ColumnDefinition Width="1*" /> 
       <ColumnDefinition Width="1*" /> 
       <ColumnDefinition Width="1*" /> 
       <ColumnDefinition Width="1*" /> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="210" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="*" /> 
      </Grid.RowDefinitions> 
      <loc:MyModuleFrame Name="mmf1" Grid.Row="1" Grid.Column="0" ItemSource="{Binding Module1}"/> 
      <loc:MyModuleFrame Name="mmf2" Grid.Row="1" Grid.Column="1" ItemSource="{Binding Module2}"/> 
      <loc:MyModuleFrame Name="mmf3" Grid.Row="1" Grid.Column="2" ItemSource="{Binding Module3}"/> 
      <loc:MyModuleFrame Name="mmf4" Grid.Row="1" Grid.Column="3" ItemSource="{Binding Module4}"/> 
      <loc:MyModuleFrame Name="mmf5" Grid.Row="1" Grid.Column="4" ItemSource="{Binding Module5}"/> 
      <loc:MyModuleFrame Name="mmf6" Grid.Row="1" Grid.Column="5" ItemSource="{Binding Module6}"/> 
     </Grid> 
    </DataTemplate> 
</Window.Resources> 
    <Grid> 
    <TabControl Grid.Row="0" Name="tabHolder" SelectedIndex="{Binding Selected}" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" Height="Auto" Width="Auto" 
       ContentTemplate="{StaticResource tabTemplate}"> 
    </TabControl> 

    </Grid> 
</Window> 

とメインウィンドウのコードビハインド:

namespace WpfApplication4 
{ 
    public partial class Window1 : Window 
    { 

    protected List<MainViewModel> viewmodels; 

    public Window1() 
    { 
     InitializeComponent(); 

     viewmodels = new List<MainViewModel>(); 
     viewmodels.Add(new MainViewModel()); 
     viewmodels.Add(new MainViewModel()); 
     viewmodels.Add(new MainViewModel()); 

     tabHolder.ItemsSource = viewmodels; 
    } 
    } 
} 

答えて

0

してください他人からより良い情報や便利な助けのためには、常にいくつかを投稿しますコード。

私は最高の私ができるように説明しようと私はそれが役に立てば幸い:

あなたはので、私はあなたが良いアプローチですMVVMを使用していることを考えるのViewModelを述べました。この方法では、バインディングを使用してUI要素にアクセスするため、各レベルにDependencyPropertyを公開する必要があります。

+0

こんにちは!フィードバックありがとう!私はコードを追加しました。 @エマド –

関連する問題