문제

괴상한 플러스 아트 오늘 블로그에는 Arrows 용 WPF 리소스 작성에 대한 게시물이있었습니다. 저자는 자주 사용합니다. 뒷면 및 전방 버튼이있는 부수적 인 프로젝트가 있으므로 왼쪽 및 오른쪽 화살표가 해당 버튼에서 잘 작동한다고 생각했습니다.

나는 그것을 추가했다 LeftArrow 그리고 RightArrow 내 응용 프로그램의 자원에 대한 형상이 된 다음 버튼의 내용으로 사용했습니다.

<Application x:Class="Notes.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Views/MainWindow.xaml">
    <Application.Resources>
        <Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry>
        <Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry>
    </Application.Resources>
</Application>

<Button x:Name="BackButton"
    Padding="5,5,5,5"
    Command="{x:Static n:Commands.GoBackCommand}">
    <Path Data="{StaticResource LeftArrow}" Width="10" Height="8"
        Stretch="Fill" Fill="Black"/>
    </Button>
<Button x:Name="ForwardButton"
    Padding="5,5,5,5"
    Command="{x:Static n:Commands.GoForwardCommand}">
    <Path Data="{StaticResource RightArrow}" Width="10" Height="8"
        Stretch="Fill" Fill="Red" />
</Button>

버튼이 활성화되었는지 여부에 관계없이 화살표가 검은 색으로 그려 졌다는 점을 제외하고는 효과가있었습니다. 그래서 나는 a ValueConverter a bool a Brush:

class EnabledColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        bool b = (bool)value;
        return b ? Brushes.Black : Brushes.Gray;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

(하드 코딩 된 검은 색과 회색 대신 시스템 색상을 사용해야한다는 것을 알고 있지만 먼저이 작업을 수행하고 싶었습니다.)

나는 수정했다 Fill 의 속성 Path 내 변환기를 사용하려면 (응용 프로그램의 리소스 내에서 만든) :

<Path Data="{StaticResource LeftArrow}" Width="10" Height="8"
    Stretch="Fill"
    Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource EnabledColorConverter}}"/>

불행히도, 이것은 작동하지 않으며 왜 그런지 잘 모르겠습니다. 내가 그것을 달리면 화살표가 전혀 그려지지 않습니다. Visual Studio에서 출력 창을 확인했으며 바인딩 오류가 표시되지 않았습니다. 나는 또한 그것을 확인했다 bool 버튼을 활성화 해야하는지 여부에 따라 변환기의 올바른 값입니다.

내가 바꾸면 Path 다시 a로 돌아갑니다 TextBlock (그리고 묶습니다 Foreground 같은 방식으로 재산 Path.Fill), 텍스트는 항상 검은 색으로 그려집니다.

내가 뭔가 잘못하고 있습니까? 왜 Brush 내 컨버터로 반품 렌더링에 사용되지 않습니다 Path 버튼에?

도움이 되었습니까?

해결책

왜 단지 버튼의 전경에 경로를 채우지 않습니까?

<Button x:Name="BackButton"
    Padding="5,5,5,5"
    Command="{x:Static n:Commands.GoBackCommand}">
    <Path Data="{StaticResource LeftArrow}" Width="10" Height="8"
        Stretch="Fill" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Foreground"/>
    </Button>

다른 팁

이러한 종류의 UI 상태 업데이트의 경우 대신 트리거를 사용해보십시오. 값 변환기를 작성하지 못하면 글을 쓰는 것이 훨씬 짧습니다.

이 시도:

<Application x:Class="Notes.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Views/MainWindow.xaml">
    <Application.Resources>
        <Geometry x:Key="RightArrow">M0,0 L1,0.5 0,1Z</Geometry>
        <Geometry x:Key="LeftArrow">M0,0.5 L1,1 1,0Z</Geometry>

         <!-- Base Arrow Style, with a trigger to toggle it Gray when its parent button is disabled -->
        <Style x:Key="ArrowStyle" TargetType="Path">
            <Setter Property="Width" Value="10"/>
            <Setter Property="Height" Value="8"/>
            <Setter Property="Stretch" Value="Fill"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled}" Value="False">
                    <Setter Property="Fill" Value="Gray"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>

        <!-- Left Arrow Style, with the Left Arrow fill and data -->
        <Style x:Key="LeftArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path">
            <Setter Property="Fill" Value="Black"/>
            <Setter Property="Data" Value="{StaticResource LeftArrow}"/>
        </Style>

        <!-- Right Arrow Style, with the Right Arrow fill and data -->
        <Style x:Key="RightArrowStyle" BasedOn="{StaticResource ArrowStyle}" TargetType="Path">
            <Setter Property="Fill" Value="Red"/>
            <Setter Property="Data" Value="{StaticResource RightArrow}"/>
        </Style>
    </Application.Resources>

    <Button x:Name="BackButton" Padding="5,5,5,5" IsEnabled="False">
        <Path Style="{StaticResource LeftArrowStyle}"/>
    </Button>
    <Button x:Name="ForwardButton" Padding="5,5,5,5">
        <Path Style="{StaticResource RightArrowStyle}"/>
    </Button>
</Application>

그런 다음 왼쪽 및 오른쪽 화살표에 대해 각각 LeftarrowStyle 및 RightarrowStyle에서 기본 채우기를 설정합니다. 경로 자체에 설정하면 그 값은 우선 순위를 차지하고 스타일이나 트리거가 할 수있는 모든 것을 무시할 수 있습니다. 기본 스타일 인 ArrowStyle에는 부모 버튼에 바인딩 된 Datatrigger가 포함되어 있습니다. ISENABLED가 False 일 때 발생하고 경로의 채우기가 회색으로 변경됩니다.

코드는 본질적으로 작동합니다. 컨버터를 얻는 위치를 지정하지 않기 때문에 정적 자원이 잘못되었을 수 있습니다.

당신은 아마 필요합니다

<Window.Resources>
    <conv:EnabledColorConverter x:Key="brushConv" />
</Window.Resources>

그런 다음 바인딩을 다음과 같이 지정하십시오.

{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=IsEnabled, Converter={StaticResource brushConv}}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top