2017-12-04 1 views
0

나는 윈도우에 ListBox와 Button을 가지고있다. ListBox에 포커스가있는 경우 Button의 IsEnabled 속성은 True 여야합니다. 여기에 내가 무엇을 시도했다입니다 :스타일 트리거를 사용하여 다른 컨트롤 바꾸기

<Style TargetType="{x:Type ListBox}"> 
    <Setter 
     Property="Margin" 
     Value="15,0,15,20"></Setter> 
     <Style.Triggers> 
      <Trigger Property="IsFocused" Value="True" /> 
      <Setter TargetName="btnActivate" Property="IsEnabled" Value="True" /> 
     </Style.Triggers> 
</Style> 

이것은 매우 간단한 것 같지만 나는

btnActivate가 인식되지 않는다는 오류 메시지가 표시됩니다.

올바른 방법은 무엇입니까?

IsEnabled="{Binding ElementName=lstInactive, Path=SelectedIndex, Converter={StaticResource cvtr}}"> 

이 변환기를 사용 : 다음과 같이

EDIT 1 나는 MoonMoo의 제안 링크를 구현

이 목록 상자가 집중 될 때의 IsEnabled를 설정하기 위해 노력하고 있지만 켜지지 않습니다
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert 
     Dim IsFocused As Integer = CInt(value) 
     Return If(IsFocused > -1, True, False) 
    End Function 

목록 상자에 포커스가 없어지면 꺼집니다. 포커스를 얻을 수있는 다른 컨트롤에 바인딩하는 멀티 바인딩을 조사해야 할 수도 있습니다.

편집 2 은 내가 노력 언급한다 : 컨버터와

IsEnabled="{Binding ElementName=lstInactive, Path=IsFocused, Converter={StaticResource cvtr}}"> 

:

Return CBool(value) 

하지만 그

+0

이와 비슷한 내용이 https://stackoverflow.com/questions/14068606/wpf-bind-a-control-visibility-to-the-focused-property-of-another-control에서 응답되었습니다. 대신 IsEnabled 속성에 바인딩 할 수 있습니다. –

+0

@MoonMoo OP에 대한 나의 편집을보세요. – SezMe

답변

0

사용할 수있는 버튼의 IsEnabled 속성을 변경하지 않았다 버튼 대신 DataTrigger을 입력하십시오.

<Button> 
    <Button.Style> 
    <Style TargetType="Button"> 
     <Setter Property="IsEnabled" Value="False"/> 
    <Style.Triggers> 
     <DataTrigger Binding="{Binding IsFocused,ElementName=YourListBoxName}" Value="True"> 
      <Setter Property="IsEnabled" Value="True"/> 
     </DataTrigger> 
    </Style.Triggers> 
    </Style> 
    </Button.Style> 
</Button> 
+0

이것은 매우 가깝지만 작동하지 않습니다. IsFocused를 사용하면 단추의 IsEnabled 속성이 변경되지 않습니다. 그래서 Focused (ListBox 문서에서 명명 된 속성이지만 해당 속성을 찾을 수 없다는 런타임 오류가 발생했습니다.) 또한 다음 주석을 참조하십시오. – SezMe

+0

그래서 모든 다른 ListBox 속성을 시도했는데 하나는 부분적으로 작동합니다 .OutIndex 값을 0으로 시도한 및 ListBox 첫 번째 항목을 클릭 할 때 작동합니다. 그래서 "-1"값을 사용하여 명백한 다음 단계를 시도했다. 런타임 오류뿐만 아니라 단추의 IsEnabled 속성을 변경하지 않았습니다. – SezMe

+0

그래서 어떤 항목이 선택되면 단추가 활성화되어 있어야하는 무언가가 필요합니까? – tabby

관련 문제