2011-08-18 40 views
0

我在銀光應用程序中使用NumericUpDown控件。代碼如下如何讀取silverlight中風格內定義的控件的屬性。

<StackPanel Style="{StaticResource StackPanelStyle_LableValue}"> 
         <TextBlock Text="{Binding Path=ViewItem.Strength, Source={StaticResource LocalizedStrings }}" Style="{StaticResource TextBlockStyle}" /> 
         <inputToolkit:NumericUpDown Style="{StaticResource NumericUpdownStyle_Strength}" 
                Value="{Binding RightSpecGlassStrength, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" 
                TabIndex="5" /> 
        </StackPanel> 

和風格,我結合NumericUpDown控件如下

<Style x:Key="NumericUpdownStyle_Strength" TargetType="inputToolkit:NumericUpDown"> 
    <Setter Property="Width" Value="Auto"></Setter> 
    <Setter Property="MinWidth" Value="50"></Setter> 
    <Setter Property="Height" Value="20"></Setter> 
    <Setter Property="Minimum" Value="-28"></Setter> 
    <Setter Property="Maximum" Value="28"></Setter>    
    <Setter Property="BorderThickness" Value="0"></Setter> 
    <Setter Property="BorderBrush" Value="White"></Setter> 
    <Setter Property="DecimalPlaces" Value="2"></Setter> 
    <Setter Property="Increment" Value="0.25"></Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="inputToolkit:NumericUpDown"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 


         <VisualStateGroup x:Name="CommonStates"> 
          <VisualStateGroup.Transitions> 
           <VisualTransition GeneratedDuration="0"/> 
          </VisualStateGroup.Transitions> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="Disabled"> 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="(UIElement.Opacity)" To="1"/> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
         <VisualStateGroup x:Name="FocusStates"> 
          <VisualState x:Name="Focused"> 
           <Storyboard> 
            <DoubleAnimationUsingKeyFrames Storyboard.TargetName="FocusVisualElement" Storyboard.TargetProperty="Opacity"> 
             <SplineDoubleKeyFrame KeyTime="0" Value="1"/> 
            </DoubleAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Unfocused"/> 
         </VisualStateGroup> 

         <VisualStateGroup x:Name="ValidationStates"> 
          <VisualState x:Name="Valid"/> 
          <VisualState x:Name="InvalidUnfocused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="InvalidFocused"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ValidationErrorElement" Storyboard.TargetProperty="Visibility"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsOpen"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <sys:Boolean>True</sys:Boolean> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 

        </VisualStateManager.VisualStateGroups> 
        <inputToolkit:ButtonSpinner x:Name="Spinner" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" MinWidth="35"> 
         <StackPanel Background="White" Orientation="Horizontal" HorizontalAlignment="Right" Width="Auto">         
          <TextBox x:Name="Text" Style="{StaticResource TextBoxStyle}" TabIndex="0" 
           BorderThickness="0" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" MinWidth="50" Width="Auto" AcceptsReturn="False" Text="{TemplateBinding Value}" TextAlignment="Left" TextWrapping="NoWrap"/> 
         </StackPanel> 
        </inputToolkit:ButtonSpinner> 
        <Border x:Name="DisabledVisualElement" IsHitTestVisible="false" Opacity="0" Background="#A5FFFFFF" CornerRadius="2.5,2.5,2.5,2.5"/> 
        <Border x:Name="FocusVisualElement" IsHitTestVisible="False" Opacity="0" BorderBrush="#FF45D6FA" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="1,1,1,1"/> 
        <Border x:Name="ValidationErrorElement" Visibility="Collapsed" BorderBrush="#FFDB000C" BorderThickness="1" CornerRadius="1"> 
         <ToolTipService.ToolTip> 
          <ToolTip x:Name="validationTooltip" Height="Auto" Width="Auto" Template="{StaticResource ValidationToolTipTemplate}" DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"> 
           <ToolTip.Triggers> 
            <EventTrigger RoutedEvent="Canvas.Loaded"> 
             <BeginStoryboard> 
              <Storyboard> 
               <ObjectAnimationUsingKeyFrames Storyboard.TargetName="validationTooltip" Storyboard.TargetProperty="IsHitTestVisible"> 
                <DiscreteObjectKeyFrame KeyTime="0"> 
                 <DiscreteObjectKeyFrame.Value> 
                  <sys:Boolean>true</sys:Boolean> 
                 </DiscreteObjectKeyFrame.Value> 
                </DiscreteObjectKeyFrame> 
               </ObjectAnimationUsingKeyFrames> 
              </Storyboard> 
             </BeginStoryboard> 
            </EventTrigger> 
           </ToolTip.Triggers> 
          </ToolTip> 
         </ToolTipService.ToolTip> 
         <Grid Height="12" HorizontalAlignment="Right" Margin="1,-4,-4,0" VerticalAlignment="Top" Width="12" Background="Transparent"> 
          <Path Fill="#FFDC000C" Margin="1,3,0,0" Data="M 1,0 L6,0 A 2,2 90 0 1 8,2 L8,7 z"/> 
          <Path Fill="#ffffff" Margin="1,3,0,0" Data="M 0,0 L2,0 L 8,6 L8,8"/> 
         </Grid> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我發現錯誤,同時運行使用上述代碼機應用生成。

爲了解決這個錯誤我想讀裏面<inputToolkit:ButtonSpinner > </inputToolkit:ButtonSpinner >「文字」輸入定義的文本框的值風格的App.xaml爲NumericUpDown控件定義。

如何在運行時從文本框中讀取值ValueChainging/ValueChanged/GotFocus/LostFocust或任何其他NumericUpDown控件事件。

回答

0

我已將LostFocus事件添加到<inputToolkit:ButtonSpinner > </inputToolkit:ButtonSpinner >內部的文本框中,並且在那裏我寫了代碼以實現我的邏輯。 :)

如下我的文本框會像

<TextBox x:Name="Text" Style="{StaticResource TextBoxStyle}" TabIndex="{TemplateBinding TabIndex}" 
            BorderThickness="0" FontFamily="{TemplateBinding FontFamily}" FontSize="{TemplateBinding FontSize}" FontStretch="{TemplateBinding FontStretch}" FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" MinWidth="50" Width="Auto" AcceptsReturn="False" Text="{TemplateBinding Value}" TextAlignment="Right" TextWrapping="NoWrap" LostFocus="Text_LostFocus"/> 
0

您可以添加事件處理程序到您的內部TextBox模板。我不記得了,你能添加它的模板,但我敢肯定,你可以這樣

<Style x:Key="MyTextBoxStyle" TargetType="{x:Type TextBox}"> 
    <EventSetter Event="ValueChainging" Handler="PathName_KeyDown" /> 
    <EventSetter Event="LostFocust" Handler="GiveFocusToMe" /> 
    .... 
</Style> 

創建的風格和比它應用到您的文本框,把斷點,看你需要

所有細節
0

inputToolkit:ButtonSpinner控制具有即Spinner

所以使用後,才能在視覺得到如下的控制(在模板的名稱樹是完全形成,如Load事件)後...

var mySpinner = myNumericUpDownControl.Template.FindName("Spinner", myNumericUpDownControl) 

myNumericUpDownControl可以直接命名爲myNumericUpDownControl在XAML中,或者您可以附加一個行爲來將微調控件從中提取出來。無論你做出什麼決定,確保你在VisualTree完全成形後做到這一點,否則FindName將失敗。

讓我知道這是否有幫助。

相關問題