首页 > 解决方案 > IValueConverter 返回整数数组

问题描述

嗨,我正在编辑日历。现在我需要用我最喜欢的日子来填满日历。
这就是我尝试过的,这就是输出

object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    int[] days = new int[] { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
    return days;
}

这是xaml:

<ContentPresenter
x:Name="NormalText"
Content="{Binding Converter={StaticResource GeorgianToPersianDate}}"
/>

在此处输入图像描述

更新:

<Style x:Key="CalendarDayButtonStyle" TargetType="CalendarDayButton">

        <Setter Property="MinWidth" Value="10" />
        <Setter Property="MinHeight" Value="10" />
        <Setter Property="FontSize" Value="12" />
        <Setter Property="Width" Value="32" />
        <Setter Property="Height" Value="32" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CalendarDayButton">
                    <ControlTemplate.Resources>
                        <local:GeorgianToPersianDate x:Key="GeorgianToPersianDate" />
                    </ControlTemplate.Resources>
                    <Grid>
                        <Rectangle
                            x:Name="TodayBackground"
                            Fill="{DynamicResource DangerBrush}"
                            Opacity="0"
                            RadiusX="16"
                            RadiusY="16" />
                        <Rectangle
                            x:Name="SelectedBackground"
                            Fill="{DynamicResource PrimaryBrush}"
                            Opacity="0"
                            RadiusX="16"
                            RadiusY="16" />
                        <ContentPresenter
                            x:Name="NormalText"
                            Content="{Binding Converter={StaticResource GeorgianToPersianDate}}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <TextElement.Foreground>
                                <SolidColorBrush Color="{DynamicResource PrimaryTextColor}" />
                            </TextElement.Foreground>
                        </ContentPresenter>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.1" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="NormalText"
                                            Storyboard.TargetProperty="Opacity"
                                            To=".35"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="SelectedBackground"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0:0:.2" />
                                        <ColorAnimation
                                            Storyboard.TargetName="NormalText"
                                            Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"
                                            To="White"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ActiveStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="Active" />
                                <VisualState x:Name="Inactive">
                                    <Storyboard>
                                        <ColorAnimation
                                            Storyboard.TargetName="NormalText"
                                            Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"
                                            To="{DynamicResource ThirdlyTextColor}"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="DayStates">
                                <VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0" />
                                </VisualStateGroup.Transitions>
                                <VisualState x:Name="RegularDay" />
                                <VisualState x:Name="Today">
                                    <Storyboard>
                                        <DoubleAnimation
                                            Storyboard.TargetName="TodayBackground"
                                            Storyboard.TargetProperty="Opacity"
                                            To="1"
                                            Duration="0" />
                                        <ColorAnimation
                                            Storyboard.TargetName="NormalText"
                                            Storyboard.TargetProperty="(TextElement.Foreground).(SolidColorBrush.Color)"
                                            To="White"
                                            Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这是我的 xaml,我只想删除默认天数并添加我自己的天数,我想我可以用转换器来做到这一点。那么如果我不能用转换器做到这一点,我怎么能改变日子呢?

标签: c#wpfxamlivalueconverter

解决方案


首先,让我们从绑定开始。

A通过属性ContentPresenter呈现它的 UI 。Content这只是一个object. XAML 有一些奇特的方法可以将某些对象转换为视觉对象,但如果它失败了,它只会调用ToString()object并显示文本。看着你的IValueConverter你正在返回一个int[]并且Content变成那个。System.Int32[]您给出的代码为什么不显示它是未知的,但这就是正在发生的事情。

所以知道了,让我们看看你的IValueConverter.

您返回的arrayint( int[])。现在是System.Int32[]你打电话的时候ToString()

如果您Template是一个包含多个项目的类型,ItemsControl那么自动分配ItemsSource会自动执行相同的操作,但针对数组中的每个项目。

的用法IValueConverter不正确。它不转换任何东西,也不是转换器的用途。您应该只对 ViewModel 或任何地方的属性进行纯绑定,但不用IValueConverter于此目的。

当您已经有一些绑定时,将使用转换器;但不想使用该绑定的结果,并且更愿意将该结果“转换”为其他内容。

例如:如果您要绑定到返回int值但希望将其转换int为相关enum名称的属性;然后您将编写一个转换器,读取int并返回相关enum名称。

修复:

很难使用您提供的 XAML,但知道有几种方法可以解决这个问题。第一的; 您至少需要一个模板,该模板将数组除外作为绑定以按原样显示它们。看着您的视图图像,我不相信您就是这种情况。我相信您已经将其ContentControl放入模板中;并且该模板是ItemsSource另一个控件的一部分。如果是这种情况,那么您将绑定ItemsSource到数组;并ContentPresenter像这样直接绑定到它的数据:

<ContentPresenter x:Name="NormalText"
                  Content="{Binding}"/>

说了这么多;如果没有看到整个 XAML,我无法以正确的方式为您提供明确的解决方案。


更新后,我认为问题在于您正在设置样式CalenderButton并将其分配给Content数组。像我上面那样写绑定;然后自定义Style并将Calendar绑定设置到数组。我从来没有做过 Calendar 样式,但我可以通过查看它来判断它Calendar有一些时尚,panel并且ItemsSource这些项目的模板是CalendarButton. 换句话说,日期是从日历分配到按钮的,而您正试图直接从按钮执行此操作。


推荐阅读