2013-11-15 2 views
2

저는 XAML의 기본 스타일 인 HandledWindow 유형의 템플릿을 포함하고 있습니다. 스타일에는 색상, 글꼴 및 나중에 사용할 수있는 다른 변수와 같은 여러 로컬 리소스가 포함되어 있습니다.WPF에서 UI 설정을 만드는 적절한 방법은 무엇입니까?

사용자를위한 UI 설정을 고려 중이므로 원하는대로 색상과 크기를 변경할 수 있습니다. 그러나 로컬 리소스를 변경해도 스타일 자체는 변경되지 않지만 현재 HandledWindow 인스턴스 만 변경된다는 것을 알았습니다. 따라서 UI 설정에 적합한 방법이 아닙니다. 애플리케이션에 실행중인 창이 더 많을 수 있기 때문입니다.

그런 다음 변수클래스의 템플릿에 변수를 바인딩해야한다는 것을 알게되었습니다. 여기에는 모든 변경 가능한 설정이 공개 & 정적 속성으로 포함됩니다. 하지만 인스턴스에 대해서만 작동하는 PropertyChanged 이벤트를 발생시킬 수 없기 때문에 정적 속성 바인딩 문제가 발생했습니다. 창은 그 스타일을 그 자체로 업데이트하지 않습니다.

또한 스타일을 다시 시작하고 즉시 다시 시작하지 않고 업데이트하려고합니다.

+1

관련 코드와 XAML을 게시하십시오. BTW WPF는 [테마에 대한 내장 지원] (http://stackoverflow.com/a/11139598/643085)을 가지고 있는데, 이것은 공룡 winforms에서 완전히 빠져 있습니다. 그래서 당신은 winforms에서 모든 종류의 끔찍한 해킹을 강요 당했고, WPF에서 모든 것이 아름답고 행복합니다. –

+0

테마 트릭은 'HandledWindow'의 현재 인스턴스 리소스를 대체하여 수행됩니다. 그 역시 생각했습니다.하지만 데이터 바인딩과 같은 순간에 다른 윈도우의 테마를 변경하는 무언가가 필요합니다. C# 코드만으로 모든 XAML 페이지, 속성에 대한 새 값 설정 및 시각적 UI가 동시에 업데이트되어야합니다. 몇 분 안에 코드를 게시 할 것입니다. 이러한 정적 공용 속성도 UI로 편집 할 수 있어야합니다. 즉, UI에서 설정하기 위해 다른 데이터 바인딩이 있어야 함을 의미합니다. – Lispwave

+1

아니요. 애플리케이션 전반의 테마를 원하면 'System.Windows.Application.Current.Resources'에 리소스 사전을로드하십시오. –

답변

1

WPF는 "리소스 중심"입니다. 리소스의 모든 UI 스타일, 브러쉬 및 템플릿을 정의하고 런타임에 언급 한 모든 속성을 포함하는 응용 프로그램 차원의 테마 변경을 매우 쉽게 수행 할 수 있습니다. 그것의 SettingsViewModel를 통해 내 설정 창에서 메시지를 수신 한 후 여기에 내 MainViewModel에서 그것을 할 방법은 다음과 같습니다 분명히

private void ApplyTheme() 
{   
    Application.Current.Resources.MergedDictionaries.Clear(); 

    var rd = new ResourceDictionary { { "Locator", locator } }; 
    Application.Current.Resources.MergedDictionaries.Add(rd); 

    switch (theme) 
    { 
     case "Blue": 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Office_Blue;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) }); 
      break; 
     case "Summer": 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute) }); 
      Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("/Telerik.Windows.Themes.Summer;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute) }); 
      break; 
     } 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Brushes.xaml", UriKind.RelativeOrAbsolute) }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/ControlTemplates.xaml", UriKind.RelativeOrAbsolute) }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/DataTemplates.xaml", UriKind.RelativeOrAbsolute) }); 
     Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("Resources/Styles.xaml", UriKind.RelativeOrAbsolute) }); 
    } 

그래서 나는 그들의 사전로드하지만, 방법의 하단에 당신이거야 제어합니다 Telerik를 사용하고 있습니다 브러시, 스타일 등의 리소스를로드합니다.

결론적으로 WPF에서는 응용 프로그램 차원의 테마 변경이 쉽지 않았습니다.

관련 문제