2009-09-25 9 views
1

나는 상속이 후하고, 일반 C# 클래스를 생성하여 도구 모음 클래스를 향상시킬 수 있습니다 :WPF에서 ToolBar를 상속하고 XAML 파일을 만들려면 어떻게해야합니까?

public class NiceToolBar : ToolBar 
{ 
    private ToolBarTray mainToolBarTray; 

    public NiceToolBar() 
    { 
     mainToolBarTray = new ToolBarTray(); 

     mainToolBarTray.IsLocked = true; 
     this.Background = new SolidColorBrush(Colors.LightGray); 
     ... 

그러나 문제는이 같은 코드에 내 모든 컨트롤을 조작하는 저를 강제로 :

ToolBar toolBar = new ToolBar(); 
toolBar.Background = new SolidColorBrush(Colors.Transparent); 
toolBar.Cursor = Cursors.Hand; 

StackPanel sp = new StackPanel(); 
sp.Orientation = Orientation.Horizontal; 

TextBlock tb = new TextBlock(); 
tb.Text = label; 
tb.Margin = new Thickness { Top = 3, Left = 3, Bottom = 3, Right = 10 }; 

Image image = new Image(); 
image.Source = new BitmapImage(new Uri("Images/computer.png", UriKind.Relative)); 

sp.Children.Add(image); 
sp.Children.Add(tb); 
toolBar.Items.Add(sp); 

내가 정말로 필요로하는 것은 XAML이 지루한 매개 변수 지정 및 레이아웃을 수행하는 것입니다.

그래서 새로운 사용자 정의 컨트롤을 만들고 다음과 같이 ToolBar를 상속하도록 코드를 변경하십시오.

그리고 내 XAML에 넣을 :

<UserControl x:Class="TestUserControl.Helpers.SmartToolBar" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Source="Images/computer.png"/> 
     <TextBlock x:Name="TheLabel"/> 
    </StackPanel> 
</UserControl> 

그러나 실행할 때

Partial declaration of "TestCallConstructor.Helpers.SmartToolBar" may not define different basis classes

내 사용자 정의 컨트롤을 xaml로 하는 방법은 무엇입니까?

답변

3

사용자 정의 컨트롤 (UserControl 아님)을 찾고 있습니다. 맞춤 컨트롤을 작성할 때 기본보기 (예 : XAML)를 지정할 수 있습니다.

WPF에서 사용자 정의 컨트롤을 만드는 방법은 Google에서 할 수 있지만 여기서는 내가 찾은 링크 중 하나가 How to Create a WPF Custom Control입니다.

희망이 있습니다. :).

3

당신은 UserControl를 작성 아닙니다. XAML은 기본 클래스를 UserControl으로 지정하고 C#은 ToolBar을 지정합니다. 분명 거기에 갈등이 있습니다.

처음부터 코드에서이 작업을 수행한다는 전제를 이해하지 못합니다. ToolBarItemsSource을 항목 모음에 바인딩하고 평소 ItemsControl 속성을 사용하여 렌더링을 제어하지 않는 이유는 무엇입니까?

관련 문제