c# - 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,我只想删除默认天数并添加我自己的天数,我想我可以用转换器来做到这一点。那么如果我不能用转换器做到这一点,我怎么能改变日子呢?
解决方案
首先,让我们从绑定开始。
A通过属性ContentPresenter
呈现它的 UI 。Content
这只是一个object
. XAML 有一些奇特的方法可以将某些对象转换为视觉对象,但如果它失败了,它只会调用ToString()
它object
并显示文本。看着你的IValueConverter
你正在返回一个int[]
并且Content
变成那个。System.Int32[]
您给出的代码为什么不显示它是未知的,但这就是正在发生的事情。
所以知道了,让我们看看你的IValueConverter
.
您返回的array
是int
( 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
. 换句话说,日期是从日历分配到按钮的,而您正试图直接从按钮执行此操作。
推荐阅读
- fortran - 警告 #10145 没有对文件“A.obj”执行任何操作
- python - pip-18.0 出现“找不到日志记录模块”错误
- javascript - 如何根据两个数字范围获取 0 - 1 的范围
- git - 包含新子模块的 Git 拉取分支
- java - 日志记录:在 Spring Boot 中使用属性文件实现 Log4j2
- kubernetes - 无法让 Traefik 使用 Kubernetes 在 Centos 7.5 上工作
- objective-c - 在 Cocoa App 中将多行标题设置为 NSButton
- mongodb - 使用错误 ID 存储的嵌入式根对象
- python - 当我使用python自动知道应用程序名称时,如何在任务管理器中找到任务名称?
- javascript - 无法加载文件或程序集“cefsharp.core”