私たちのためによく働いています何かがあなたのアプリケーション(以下私の例ではYourPermissionsViewModel)の許可構造を表すビューモデルを定義することです。
次に、任意のパネル(この例ではStackPanel)を拡張するカスタムパネルコントロールを作成できます。これにより、IsReadOnlyプロパティのバインディングを追加し、パネルの子に保持することができます。ここで
は、XAMLでのパネルは次のようになります:
<local:PanelExtension IsEnabled="{Binding YourPermissionsViewModel.IsEnabled}"
IsReadOnly="{Binding YourPermissionsViewModel.IsReadOnly}">
<TextBox Text="eeny" Width="100" />
<TextBox Text="meeny" Width="100"/>
<TextBox Text="miny" Width="100"/>
<TextBox Text="mo" Width="100" />
<Label Content="coolio" Width="100" />
</local:PanelExtension>
そして、ここでのStackPanel延長のStackPanelのすべての機能を含む制御ならびに任意に対応するプロパティ値を更新したカスタムIsReadOnlyの依存関係プロパティを取り付けていますその財産所有する子コントロール:あなたが必要として多くのカスタマイズされたプロパティを追加することができます。このアプローチでは
public class PanelExtension : StackPanel
{
public bool IsReadOnly
{
get { return (bool)GetValue(IsReadOnlyProperty); }
set { SetValue(IsReadOnlyProperty, value); }
}
public static readonly DependencyProperty IsReadOnlyProperty =
DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(PanelExtension),
new PropertyMetadata(new PropertyChangedCallback(OnIsReadOnlyChanged)));
private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((PanelExtension)d).OnIsReadOnlyChanged(e);
}
protected virtual void OnIsReadOnlyChanged(DependencyPropertyChangedEventArgs e)
{
this.SetIsEnabledOfChildren();
}
public PanelExtension()
{
this.Loaded += new RoutedEventHandler(PanelExtension_Loaded);
}
void PanelExtension_Loaded(object sender, RoutedEventArgs e)
{
this.SetIsEnabledOfChildren();
}
private void SetIsEnabledOfChildren()
{
foreach (UIElement child in this.Children)
{
var readOnlyProperty = child.GetType().GetProperties().Where(prop => prop.Name.Equals("IsReadOnly")).FirstOrDefault();
readOnlyProperty.SetValue(child, this.IsReadOnly, null);
}
}
}
は、それは本当にあなたに大きな柔軟性を貸すとを占めることができますさまざまな要素に対して複雑な権限を設定する必要がある場合に発生する多数のシナリオです。
これは私が見つけた最良の解決策です:http://dkowalski.com/blog/archive/2010/02/04/readonly-wpf-containersaspx – surfen