2013-02-21 51 views
2

我正在學習WPF和XAML。但我是新手,有一個問題。在日曆中更改所選日背景(或文本)

我的目的 - 在日曆中更改選定日期的文本。現在,它有藍色背景。但我想兩個支架:(...):

image

我看到這個問題的兩個解決方案。

首先。更改控制模板。我發現full template code on MSDN site。但是有太多的代碼,我找不到任何方法來替換背景顏色到括號,因爲我找不到「文本」屬性。如果我找到它,我只需將其替換爲水平的StackPanel,並在日期前後添加兩個帶有「(」和「)」的TextBox。但我不能。如果我可以,我真的應該複製所有這些代碼?

二。據我所知,日曆中的所有日子都是CalendarDayButton類型。這種類型具有「上下文」屬性。也許,如果我改變它,那麼文本就會改變。我不知道。但是我沒有直接訪問這種類型。我應該讓所有的兒童日曆項目和搜索日中的其中一個。我覺得這太複雜了。

如果你知道這個問題更優雅的解決方案 - 我會非常感謝你。

回答

1

對,所以你用第一個觀察來回答你的問題。因此,如果我們在給定的MSDN示例中查看該代碼並專注於您的CalendarButtonStyle模板,則可以按照從上到下的順序來分解它的工作原理。在下面的複製示例中查看註釋。

  <!-- ********** 
      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與「()`周圍和隱藏,這並不擁有它的人。

無論如何這應該給你一個相當不錯的方向,希望它有幫助。

+0

謝謝!這真的是很好的答案。所以,我明白它是如何工作的,但是......有些不對。我在CalendarDayButtonStyle(不是CalendarButtonStyle)中找到了「」並將其替換爲您的代碼。但是我有一個運行時InvalidOperationException:「無法解析屬性路徑'TextElement'中的所有屬性引用,請驗證適用對象是否支持這些屬性。」它發生在行上:「CustomCalendarControl.SelectedDate = CustomDateTime;」。奇怪的是,CalendarDayButton是Buton類型的繼承者,應該具有「TextElement」屬性。 – DeniDoman 2013-02-22 21:10:37

+0

沒問題,請參閱編輯SelectedState並替換ContentPresenter。希望這可以幫助。 – 2013-02-22 21:25:26

+0

而新錯誤:「不可變對象實例」上的「無動畫」(0)。(1)。我發現[7歲的問題](http://blogs.msdn.com/b/mikehillberg/archive/2006/09/26/cannotanimateimmutableobjectinstance.aspx),但這種行爲有很多原因。 – DeniDoman 2013-02-22 21:56:27

相關問題