2013-03-26 2 views
2

'IsVisibleWhenReadOnly'단추에 대한 부울 속성을 만들려고합니다. StackPanel의 버튼에이 버튼을 사용하기를 원합니다. 따라서 데이터가 ReadOnly 상태에 있는지 여부에 따라 표시 여부를 결정할 수 있습니다. 즉, ReadOnly 상태 일 때 저장 및 취소 버튼은 숨겨져 있지만 편집 버튼은 보이기입니다. 편집 버튼을 클릭하면 ReadOnly 상태가 false가되고 취소 및 저장 버튼이 표시되고 편집 버튼이 숨겨집니다. 단추에 대한 종속성 속성

내 속성 코드 :

public bool IsVisibleWhenReadOnly 
{ 
    get { return (bool)GetValue(IsVisibleWhenReadOnlyProperty); } 
    set { SetValue(IsVisibleWhenReadOnlyProperty, value); } 
} 

// Using a DependencyProperty as the backing store for IsVisibleWhenReadOnly. 
public static readonly DependencyProperty IsVisibleWhenReadOnlyProperty = 
    DependencyProperty.Register("IsVisibleWhenReadOnly", 
           typeof(bool), 
           typeof(Button), 
           new PropertyMetadata(true)); 

버튼 스타일 :

<Style TargetType="{x:Type Button}"> 
    <Setter Property="Visibility"> 
    <Setter.Value> 
     <Binding Path="IsVisibleWhenReadOnly" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=Window}" Mode="OneWay"> 
     <Binding.Converter> 
      <utils:BoolToVisibilityConverter/> 
     </Binding.Converter> 
     </Binding> 
    </Setter.Value> 
    </Setter> 
</Style> 

버튼 코드 :

<Button Name="btnEdit" Content="Edit" MinWidth="75" Height="25" 
    Click="btnEdit_Click" IsVisibleWhenReadOnly="true" /> 

IsReadOnly 즐겁게 일하고 또 다른 종속성 속성이며, 수/비활성화 가치에 따라 컨트롤을 조정할 수 있지만이 기능을 사용 가능성이 아닌 가시성에 영향을 미치기 원합니다.

불행하게도, 나는 컴파일에 세 가지 오류 받고 있어요 :

The member "IsVisibleWhenReadOnly" is not recognized or is not accessible. 
The property 'IsVisibleWhenReadOnly' does not exist in XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'. 
The property 'IsVisibleWhenReadOnly' was not found in type 'Button'. 

나는 그것이 typeOf(Button), 라인에 같은데요,하지만 내 'isReadOnly의에 대한 typeOf() 값'BaseWindow '(해당 변경 '속성) 차이를 만들지 않았다. 나는 또한 내 BoolToVisibilityConverter가 문제가 아니라고 확신한다.

누구나 내가 뭘 잘못하고 올바른 방향으로 나를 가리킬 수 있습니까?

편집 : 가능한 경우에만 버튼 이상으로 종속성 속성을 사용하고 싶습니다. 예 : StackPanels, CheckBoxes 등, Buttons에만 국한되지 않는 솔루션이 이상적입니다.

+0

어디에서 'DependencyProperty'를 구현 했습니까? 'Button' 클래스를 확장 했습니까? – DHN

+0

DP는 모든 Windows가 상속하는 BaseWindow.cs로 구현됩니다. 나는 Button을 확장하지 않았습니다 - DP를 읽을 때이 지침을 보지 못했습니다. – mcalex

+0

그렇다면 이미 답변이 나와있는 것처럼 'AttachedProperty'를 사용해야합니다. – DHN

답변

1

기존 컨트롤에 피기 백하려면 attached dependency property을 사용해야합니다. 그래서 같은 :

public static void SetIsVisibleWhenReadOnly(UIElement element, bool value) 
{ 
    element.SetValue(IsVisibleWhenReadOnlyProperty, value); 
} 
public static bool GetIsVisibleWhenReadOnly(UIElement element) 
{ 
    return (bool)element.GetValue(IsVisibleWhenReadOnlyProperty); 
} 
// Using a Registerd DependencyProperty as the backing store for IsVisibleWhenReadOnly. 
public static readonly DependencyProperty IsVisibleWhenReadOnlyProperty = 
     DependencyProperty.RegisterAttached("IsVisibleWhenReadOnly", 
            typeof(bool), 
            typeof(Button), 
            new PropertyMetadata(true)); 

는 또한 반환 할 가시성 값을 결정 할 때 isReadOnly의 및 IsVisibileWhenReadOnly에 액세스 할 수 있도록 가시성을 변환 멀티 바인딩을 고려할 것입니다.

1

당신은 당신의 DependancyPropertyAttachedProperty int로서 당신은 다른 컨트롤 대신 typeof(Button)

public static readonly DependencyProperty IsVisibleWhenReadOnlyProperty = 
    DependencyProperty.RegisterAttached("IsVisibleWhenReadOnly" 
    , typeof(bool), typeof(FrameworkElement),new PropertyMetadata(true)); 

typeof(FrameworkElement)를 사용하여 그것을 사용하려면 등록을해야합니다 그러나이 DataTrigger

를 사용하는 간단한 아이디어가 될 수있다

예 :

<Button> 
    <Style TargetType="{x:Type Button}"> 
     <Setter Property="Visibility" Value="Visible" /> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsVisibleWhenReadOnly}" Value="True" > 
       <Setter Property="Visibility" Value="Collapsed" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Button> 
+0

나는 AttachedProperty를 만들었고 DataTrigger를 사용하거나 사용하지 않고 시도했다. 세 번째 예외가 중지되었지만 다른 두 개는 계속 표시됩니다. 'RegisterAttached'보다 AttachedProperties에 더 많은 것이 있습니까?아니면 다른 것을 놓치고 있습니까? – mcalex

+0

그렇게 생각하지 마십시오. 게시하기 전에 위 코드를 테스트했는데 모두 정상적으로 작동했습니다. –