2013-03-05 4 views
0

맞춤 이미지 버튼을 만들고 스타일을 추가했습니다. 모든 것이 잘 작동합니다. 하지만 다른 버튼에서 다시 사용하려고하면 잘 작동하지 않습니다. 버튼 하나만 스타일을 따르고 나머지는 항상 비어 있습니다.Silverlight에서 스타일을 다시 사용할 수 없음 5

런타임에 오류가 발생하지 않았습니다. 도와주세요!!

버튼 :

public class ImageTextButton : Button 
{ 
    public DependencyProperty TextProperty { get; set; } 
    public DependencyProperty ImageProperty { get; set; } 

    public ImageTextButton() 
    { 
     TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ImageTextButton), null); 
     ImageProperty = DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ImageTextButton), null); 
    } 

    public ImageSource ImageSource 
    { 
     get { return (ImageSource)GetValue(ImageProperty); } 
     set { SetValue(ImageProperty, value); } 
    } 

    public string Text { get; set; } 
} 

XAML :

<Controls:ImageTextButton HorizontalAlignment="Left" Command="{Binding SendCustomerChanges}" Style="{StaticResource ImageTextButton}" IsEnabled="{Binding Path=DetailsInformation.IsAllowSave}" 
          Text="{Binding Path=CustomerResources.Button_CloseDetailedView, Source={StaticResource ResourceWrapper}}" ImageSource="{Binding Path=SaveIcon, Source={StaticResource ApplicationIcons}}" /> 

스타일 :

<Style TargetType="Controls:ImageTextButton" x:Key="ImageTextButton" > 
    <Setter Property="FontFamily" Value="Arial"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Controls:ImageTextButton"> 
       <Grid> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommomStates"> 
          <VisualState x:Name="Normal"/> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ColorAnimation Duration="0" To="LightGray" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rectangle"/> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="Disabled" > 
           <Storyboard> 
            <DoubleAnimation Duration="0" Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1"/> 
            <ColorAnimation Duration="0" To="#AAAAAA" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="textBlock" /> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Rectangle x:Name="rectangle" Fill="#E0E9F1" StrokeThickness="1" Stroke="#728DA3"/> 
        <Rectangle x:Name="DisabledVisualElement" Fill="#F5F5F5" IsHitTestVisible="false" StrokeThickness="1" Stroke="#D3D3D3" Opacity="0" /> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition></ColumnDefinition> 
          <ColumnDefinition></ColumnDefinition> 
         </Grid.ColumnDefinitions> 
         <Image Grid.Column="0" Width="12" Height="12" Margin="2,0,2,0" Source="{TemplateBinding ImageSource}"> 
         </Image> 
         <TextBlock Grid.Column="1" x:Name="textBlock" Margin="2,3,4,0" Text="{TemplateBinding Text}" /> 
        </Grid> 
        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="5,3,5,3"/> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
여기 들으

코드입니다

답변

3

변경/수정해야 할 몇 가지 사항이 있습니다.

  1. 텍스트 가독성 속성이 잘못 구성되었습니다. DP 용 getter 및 setter를 정의해야합니다 (ImageSourceProperty로 수행 한 것처럼).

  2. 당신의 ImageSource 종속성 속성은 스타일을 실제로 적용되도록 ... 민주당은 ImageSourceProperty 이름을 지정해야합니다 ... described hereif they are to work under all conditions. Especially so when you are setting them from XAML

  3. Dependency Properties must follow the pattern 당신은 당신의 새로운 클래스의 default style key 필요도 몇 가지주의가 필요

  4. 귀하의 종속성 속성

    정적 생성자에 등록 (또는 필드 이니셜 라이저로해야합니다. 아니 인스턴스 생성자.

+0

예, 작동합니다. 정말 고맙습니다!! – aiyagaze

관련 문제