【问题标题】:Change selected day background (or text) in Calendar更改日历中选定的日期背景(或文本)
【发布时间】:2013-02-21 21:00:29
【问题描述】:

我正在学习 WPF 和 XAML。但我是新手,有一个问题。

我的目的 - 更改日历中选定日期的文本。现在,它有蓝色背景。但我想要两个括号:(...):

我看到了这个问题的两个解决方案。

首先。更改控制模板。我找到了full template code on MSDN site。但是代码太多,我找不到任何方法将背景颜色替换为括号,因为我找不到“文本”属性。如果我找到它,我只需将其替换为水平 StackPanel 并在日期前后添加两个带有“(”和“)”的文本框。但我不能。如果可以的话,我真的应该复制所有这些代码吗?

第二。据我所知,日历中的所有日子都是 CalendarDayButton 类型。此类型具有“上下文”属性。也许,如果我改变它,那么文本就会改变。我不知道。但我没有直接访问这种类型。我应该获取日历的所有子项并在其中搜索选定的日期。我觉得太复杂了。

如果您知道这个问题的更优雅的解决方案 - 我将非常感谢您。

【问题讨论】:

    标签: c# .net wpf xaml user-interface


    【解决方案1】:

    是的,所以你用第一个观察回答了你的问题。因此,如果我们查看给出的 MSDN 示例中的代码并专注于您的 CalendarButton Style 模板,您可以从上到下分解它是如何工作的。请参阅下面复制示例中的 cmets。

             <!-- **********
                Ok, so we know this is the bugger we want to deal with.
                -->
                <Style TargetType="CalendarButton"
                       x:Key="CalendarButtonStyle">
                  <Setter Property="MinWidth"
                          Value="40" />
                  <Setter Property="MinHeight"
                          Value="42" />
                  <Setter Property="FontSize"
                          Value="10" />
                  <Setter Property="HorizontalContentAlignment"
                          Value="Center" />
                  <Setter Property="VerticalContentAlignment"
                          Value="Center" />
                  <Setter Property="Template">
                    <Setter.Value>
                      <ControlTemplate TargetType="CalendarButton">
                        <Grid>
                <!-- *************
                What we want to change is the stuff that happens
            for a State, in this case the Selected State. So we just
            go look at what exactly it is doing for that Selected State.
                -->
                          <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">
                              <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0:0:0.1" />
                              </VisualStateGroup.Transitions>
                              <VisualState Name="Normal" />
                              <VisualState Name="MouseOver">
                                <Storyboard>
                                  <DoubleAnimation Storyboard.TargetName="Background"
                                                   Storyboard.TargetProperty="Opacity"
                                                   To=".5"
                                                   Duration="0" />
                                </Storyboard>
                              </VisualState>
                              <VisualState Name="Pressed">
                                <Storyboard>
                                  <DoubleAnimation Storyboard.TargetName="Background"
                                                   Storyboard.TargetProperty="Opacity"
                                                   To=".5"
                                                   Duration="0" />
                                </Storyboard>
                              </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup Name="SelectionStates">
                              <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                              </VisualStateGroup.Transitions>
                              <VisualState Name="Unselected" />
                <!-- ************
            Hey, look at that. So this guy's telling something called "SelectedBackground" 
            with some opacity to show up and be seen. Now we know we need to go check out 
            this SelectedBackground object he's talking to.
                -->
                              <VisualState Name="Selected">
                                <Storyboard>
                                  <DoubleAnimation Storyboard.TargetName="SelectedBackground"
                                                   Storyboard.TargetProperty="Opacity"
                                                   To=".75"
                                                   Duration="0" />
                                </Storyboard>
                              </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup Name="ActiveStates">
                              <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                              </VisualStateGroup.Transitions>
                              <VisualState Name="Active" />
                              <VisualState Name="Inactive">
                                <Storyboard>
                                  <ColorAnimation Duration="0"
                                                  Storyboard.TargetName="NormalText"
                                                  Storyboard.TargetProperty="(TextElement.Foreground).
                                      (SolidColorBrush.Color)"
                                                  To="#FF777777" />
                                </Storyboard>
                              </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup Name="CalendarButtonFocusStates">
                              <VisualStateGroup.Transitions>
                                <VisualTransition GeneratedDuration="0" />
                              </VisualStateGroup.Transitions>
                              <VisualState Name="CalendarButtonFocused">
                                <Storyboard>
                                  <ObjectAnimationUsingKeyFrames Duration="0"
                                                                 Storyboard.TargetName="CalendarButtonFocusVisual"
                                                                 Storyboard.TargetProperty="Visibility">
                                    <DiscreteObjectKeyFrame KeyTime="0">
                                      <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Visible</Visibility>
                                      </DiscreteObjectKeyFrame.Value>
                                    </DiscreteObjectKeyFrame>
                                  </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                              </VisualState>
                              <VisualState Name="CalendarButtonUnfocused" />
                            </VisualStateGroup>
                          </VisualStateManager.VisualStateGroups>
                <!-- ***************
                Sneaky bugger... looks like there is something called SelectedBackground
     (Rectangle) sitting here with a 0 opacity 
    waiting to be told to show himself. 
        Imagine that.
                -->
                          <Rectangle x:Name="SelectedBackground"
                                     RadiusX="1"
                                     RadiusY="1"
                                     Opacity="0">
                            <Rectangle.Fill>
                              <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" />
                            </Rectangle.Fill>
                          </Rectangle>
                          <Rectangle x:Name="Background"
                                     RadiusX="1"
                                     RadiusY="1"
                                     Opacity="0">
                            <Rectangle.Fill>
                              <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" />
                            </Rectangle.Fill>
                          </Rectangle>
                          <ContentPresenter x:Name="NormalText"
                                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                            Margin="1,0,1,1">
                            <TextElement.Foreground>
                              <SolidColorBrush Color="#FF333333" />
                            </TextElement.Foreground>
                          </ContentPresenter>
                          <Rectangle x:Name="CalendarButtonFocusVisual"
                                     Visibility="Collapsed"
                                     IsHitTestVisible="false"
                                     RadiusX="1"
                                     RadiusY="1">
                            <Rectangle.Stroke>
                              <SolidColorBrush Color="{DynamicResource SelectedBackgroundColor}" />
                            </Rectangle.Stroke>
                          </Rectangle>
                        </Grid>
                      </ControlTemplate>
                    </Setter.Value>
                  </Setter>
                  <Setter Property="Background">
                    <Setter.Value>
                      <SolidColorBrush Color="{DynamicResource ControlMediumColor}" />
                    </Setter.Value>
                  </Setter>
                </Style>
    

    所以你找到了最初的罪魁祸首。您可以删除 Rectangle 或将其改为 Transparent 或其他内容。然后回到你指出的VisualStateManager 中的SelectedState 声明。 编辑: 我们将采用不同的方法并显示另一个元素。;

    <VisualState Name="Selected">
       <Storyboard>
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="SelectedText"
                                         Storyboard.TargetProperty="Visibility">
                 <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}"/>
          </ObjectAnimationUsingKeyFrames>
          <ObjectAnimationUsingKeyFrames Storyboard.TargetName="NormalText"
                                         Storyboard.TargetProperty="Visibility">
                 <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Collapsed}"/>
          </ObjectAnimationUsingKeyFrames>
       </Storyboard>
    </VisualState>
    

    编辑:

    <!--
    <ContentPresenter x:Name="NormalText"
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                      Margin="1,0,1,1">
                        <TextElement.Foreground>
                          <SolidColorBrush Color="#FF333333" />
                        </TextElement.Foreground>
                      </ContentPresenter>
    -->
    <TextBlock x:Name="NormalText"
               Text="{TemplateBinding Content}"
               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
               VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
               Margin="1,0,1,1"/>
    <TextBlock x:Name="SelectedText" Visibility="Collapsed"
               Text="{TemplateBinding Content, StringFormat='(\{\0})'}"
               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
               VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
               Margin="1,0,1,1"/>
    

    现在当按钮在SelectedState 中时,它应该显示TextBlock,并在其周围显示'( )`,并隐藏没有它的那个。

    无论如何,这应该会给您一个相当不错的方向,希望对您有所帮助。

    【讨论】:

    • 谢谢!这真的是很棒的答案。所以,我明白它是如何工作的,但是......出了点问题。我在 CalendarDayButtonStyle(不是 CalendarButtonStyle)中找到了“”并将其替换为您的代码。但我有一个运行时 InvalidOperationException:“无法解析属性路径'TextElement'中的所有属性引用。验证适用的对象是否支持这些属性。”它在线发生:“CustomCalendarControl.SelectedDate = CustomDateTime;”。很奇怪,因为 CalendarDayButton 是 Button 类型的继承者,应该有“TextElement”属性。
    • 没问题,请参阅编辑 SelectedState 并替换 ContentPresenter。希望这会有所帮助。
    • 还有新错误:“Сannot animate '(0).(1)' on an immutable object instance”。我找到了7-year old issue,但是这种行为有很多原因。
    • 是的,不可变的,抱歉,我没有在进行时进行测试,实际上只是徒手调整您的模板示例。尝试编辑,这次有点不同。
    • 错误:“在类型 'TemplateBindingExtension' 中找不到属性 'StringFormat'”。但我认为,我已经找到了这个问题的解决方案:StringFormat in TemplateBinding。我尝试修复它。并在这里写下结果。
    猜你喜欢
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    • 2016-03-31
    • 2014-03-22
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    相关资源
    最近更新 更多