2011-03-08 6 views
0

일부 사용자 지정 컨트롤 스타일에서 개인 값을 설정하여 사용자가 자신의 응용 프로그램 기본 설정을 정의 할 수있게하는 방법은 무엇입니까?사용자가 런타임에 컨트롤 스타일을 편집 할 수있게하려면 어떻게해야합니까?

<UserControl.Resources> 
    <Style TargetType="{x:Type cc:MyControl}"> 
      <Setter Property="SWidth" Value="20" /> 
      ... 
      <Setter Property="SBrush" Value="Blue" />    
    </Style> 
</UserControl.Resources> 

을하지만 어떻게 런타임에 이러한 스타일의 값을 편집 :

우리는 디자인 타임에 XAML에서 그들을 설정할 수 있습니까?

답변

1

스타일의 값을 클래스의 값을 정의하는 클래스에 따라 수정할 수있는 일부 정적 클래스 (예 : 응용 프로그램의 기본 설정)에 바인딩 할 수 있습니다.

아래 앱에서 Settings.settings 파일에 FontSize이라는 속성을 만들었습니다. 내가 XAML 파일에서 적절한 네임 스페이스를 추가하고 내가 원하는대로 지금에 결합 할 수 있습니다

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:my="clr-namespace:WpfApplication1" 
     xmlns:prop="clr-namespace:WpfApplication1.Properties" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="auto" /> 
      <RowDefinition Height="auto" /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <Grid.Resources> 
      <Style TargetType="TextBlock" x:Key="myStyle"> 
       <Setter Property="FontSize" Value="{Binding FontSize, Source={x:Static prop:Settings.Default}}" /> 
      </Style> 
     </Grid.Resources> 

     <TextBlock Style="{DynamicResource myStyle}" Text="The quick brown fox jumped over the lazy dog." /> 

     <TextBox Grid.Row="1" Text="{Binding FontSize, Source={x:Static prop:Settings.Default}, UpdateSourceTrigger=PropertyChanged}" /> 
    </Grid> 
</Window> 

가 나는 TextBox에 직접 값을 바인딩하지만 말할 것도 해당 인스턴스에 대한 뷰 모델에서 일부 제어 메커니즘, 강력히 권장합니다.

private void Application_Exit(object sender, ExitEventArgs e) 
{ 
    WpfApplication1.Properties.Settings.Default.Save(); 
} 
: 설정을 저장하려면

마지막으로, 당신이해야 할 모든 응용 프로그램의 Exit 이벤트의 이벤트 핸들러, 예를 들어, 클래스의 Save 메서드를 호출입니다

관련 문제