سؤال

على ال Nerd Plus Art. بلوق اليوم، كان هناك وظيفة حول إنشاء موارد 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>

هذا يعمل، باستثناء أن السهام تم رسمها باللون الأسود بغض النظر عما إذا كان الزر ممكن أم لا. لذلك، أنا خلقت ValueConverter للذهاب من bool إلى أ 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 العودة إلى 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 خطأ، ويغير ملء المسار إلى اللون الرمادي.

الكود الخاص بك يعمل أساسا. أعتقد أن مورد ثابت الخاص بك قد يكون مخطئا مثل عدم تحديد مكانك حيث يكون هذا هو الحصول على المحول من.

ربما تحتاج

<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