IT story

이벤트 처리를 위해 WPF에서 리소스 사전 뒤에 코드를 설정할 수 있습니까?

hot-time 2020. 6. 15. 08:10
반응형

이벤트 처리를 위해 WPF에서 리소스 사전 뒤에 코드를 설정할 수 있습니까?


WPF에서 리소스 사전 뒤에 코드를 설정할 수 있습니까? 예를 들어 버튼의 사용자 컨트롤에서 XAML로 선언합니다. 버튼 클릭에 대한 이벤트 처리 코드는 컨트롤 뒤의 코드 파일에서 수행됩니다. 버튼으로 데이터 템플릿을 만들려면 리소스 사전 내에서 버튼 클릭에 대한 이벤트 처리기 코드를 작성하는 방법은 무엇입니까?


당신이 묻는 것은 ResourceDictionary에 대한 코드 숨김 파일을 원한다고 생각합니다. 당신은 완전히 이것을 할 수 있습니다! 사실, 당신은 Window와 같은 방식으로 수행합니다 :

MyResourceDictionary라는 ResourceDictionary가 있다고 가정하십시오. MyResourceDictionary.xaml 파일에서 다음과 같이 x : Class 속성을 루트 요소에 넣으십시오.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    x:Class="MyCompany.MyProject.MyResourceDictionary"
                    x:ClassModifier="public">

그런 다음 다음 선언으로 MyResourceDictionary.xaml.cs라는 파일 뒤에 코드를 작성하십시오.

namespace MyCompany.MyProject
{
    partial class MyResourceDictionary : ResourceDictionary
    { 
       public MyResourceDictionary()
       {
          InitializeComponent();
       }     
       ... // event handlers ahead..
    }
}

그리고 당신은 끝났습니다. 메서드, 속성 및 이벤트 처리기 등 원하는 코드를 코드 뒤에 넣을 수 있습니다.

== Windows 10 앱용 업데이트 ==

그리고 UWP를 가지고 노는 경우를 대비 하여 알아야 할 것이 하나 더 있습니다.

<Application x:Class="SampleProject.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:rd="using:MyCompany.MyProject">
<!-- no need in x:ClassModifier="public" in the header above -->

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!-- This will NOT work -->
                <!-- <ResourceDictionary Source="/MyResourceDictionary.xaml" />-->

                <!-- Create instance of your custom dictionary instead of the above source reference -->
                <rd:MyResourceDictionary />

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application>

"ageektrapped"에 동의하지 않습니다. 부분 클래스의 방법을 사용하는 것은 좋은 습관이 아닙니다. 그러면 페이지에서 사전을 분리하는 목적은 무엇입니까?

코드 숨김에서 다음을 사용하여 ax : Name 요소에 액세스 할 수 있습니다.

Button myButton = this.GetTemplateChild("ButtonName") as Button;
if(myButton != null){
   ...
}

You can do this in the OnApplyTemplate method if you want to hookup to controls when your custom control loads. OnApplyTemplate needs to be overridden to do this. This is a common practice and allows your style to stay disconnected from the control. (The style should not depend on the control, but the control should depend on having a style).


Gishu - whilst this might seem to be a "generally not to be encouraged practice" Here is one reason you might want to do it:

The standard behaviour for text boxes when they get focus is for the caret to be placed at the same position that it was when the control lost focus. If you would prefer throughout your application that when the user tabs to any textbox that the whole content of the textbox was highlighted then adding a simple handler in the resource dictionary would do the trick.

Any other reason where you want the default user interaction behaviour to be different from the out of the box behaviour seems like good candidates for a code behind in a resource dictionary.

Totally agree that anything which is application functionality specific ought not be in a code behind of a resource dictionary.


XAML is for constructing object graphs not containing code.
A Data template is used to indicate how a custom user-object is to be rendered on screen... (e.g. if it is a listbox item) behavior is not part of a data template's area of expertise. Redraw the solution...

참고URL : https://stackoverflow.com/questions/92100/is-it-possible-to-set-code-behind-a-resource-dictionary-in-wpf-for-event-handlin

반응형