wpf custom control 不显示

fengyoujie 2012-10-24 05:16:44
各位老师好!

我做了一个wpf的自定义控件(custom control)。
Generic.xaml文件代码如下:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:FileSelectorControl">
<Style TargetType="{x:Type local:FileSelector}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:FileSelector}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">

<ScrollViewer Name="scrolls" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto">
<ScrollViewer.Content>
<StackPanel Name= "multi_item" Orientation="Vertical" ScrollViewer.VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Horizontal" Height="30" Margin="0,5,0,0">
<Button Name="add_item" Content="增加" Margin="5,0,0,0" Width="80" ></Button>
<Button Name="del_item" Content="删除" Margin="5,0,0,0" Width="80" />
</StackPanel>

<StackPanel Name="stack_static" Orientation="Horizontal" Height="30" Margin="0,5,0,0">
<TextBox Name="textBox_FileFullName" Margin="5,0,5,0" Width="330"></TextBox>
<Label Content="名称" Margin="5,0,5,0"></Label>
<TextBox Name="displayName" Margin="5,0,5,0" Width="200"></TextBox>

<Button Name="button_browser" Content="浏览"/>
</StackPanel>

<StackPanel Name="dynamic_item">


</StackPanel>
</StackPanel>
</ScrollViewer.Content>
</ScrollViewer>


</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>


类的代码如下FileSelector.cs

using System;
using System.Linq; //程序用到LINQ
using System.Windows; //TemplatePartAttribute
using System.Collections.Generic;
using System.Windows.Controls; //Control
using System.Windows.Input; //RoutedUICommand
using Microsoft.Win32; //对话框OpenFileDialog

namespace FileSelectorControl
{
[TemplatePart(Name = "textBox_FileFullName", Type = typeof(TextBox))]
public class FileSelector : Control
{
static FileSelector()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FileSelector), new FrameworkPropertyMetadata(typeof(FileSelector)));
}

public FileSelector():base()
{

}


#region 控件模板


Button add_item;
Button del_item;
Button btn1;
StackPanel multi_item;
StackPanel dynamic_item;

TextBox tbx ;

public override void OnApplyTemplate()
{
base.OnApplyTemplate();

add_item = Template.FindName("add_item", this) as Button;
del_item = Template.FindName("del_item", this) as Button;

add_item.Click += new RoutedEventHandler(add_item_click);
del_item.Click += new RoutedEventHandler(del_item_click);

btn1 = Template.FindName("button_browser",this) as Button;
btn1.Click += new RoutedEventHandler(btn_click);

multi_item = Template.FindName("stackPanel_multiItem", this) as StackPanel;
dynamic_item = Template.FindName("stackPanel_dynamicItem", this) as StackPanel;


tbx = Template.FindName("textBox_FileFullName", this) as TextBox;
}

#endregion

#region 对话框处理

public string PredefinedExts { get; set; }

string GetFilterString(out string first)
{
first = null;
var exts = PredefinedExts.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
string head = string.Empty;
if (exts.Length != 0)
{
var trim = exts.Where(s => s.Trim() != String.Empty);
first = trim.FirstOrDefault();
head = String.Join("|", trim.Select(s => String.Format("{0}文件|*.{1}", s.ToUpper(), s.ToLower())));
}

if (head != String.Empty)
head += "|";

return head + "所有文件|*.*";
}

string OpenDialog()
{
var dlg = new OpenFileDialog();
string defExt;
dlg.Filter = GetFilterString(out defExt);
dlg.DefaultExt = defExt;

if (dlg.ShowDialog() == true)
return dlg.FileName;
return null;
}

#endregion





void add_item_click(object sender, RoutedEventArgs e)
{
StackPanel panel = new StackPanel();
panel.Orientation = Orientation.Horizontal;
panel.Margin = new Thickness(0, 5, 0, 0);

TextBox tbx = new TextBox();
tbx.Width = 330;
tbx.Height = 30;

tbx.Margin = new Thickness(5, 0, 5, 0);
Button btn = new Button();
btn.Content = "浏览";
btn.Click += new RoutedEventHandler(btn_click);

panel.Children.Add(tbx);
panel.Children.Add(btn);

//multi_item.Children.Remove();

dynamic_item.Children.Add(panel);

}


void del_item_click(object sender, RoutedEventArgs e)
{
int count = dynamic_item.Children.Count;
if(count>0){
dynamic_item.Children.RemoveAt(count-1);
}
}

void btn_click(object sender,RoutedEventArgs e) {
string path = OpenDialog();
Button btn = sender as Button;
StackPanel parent = btn.Parent as StackPanel;

foreach(var child in parent.Children){
if(child is TextBox){
((TextBox)child).Text = path;
}
}
}

}
}



工程编译完事之后,在toolbox能看到自定义控件的图标,但是当在其他的xaml文件中引用FileSelector的时候,不显示这个控件。

主窗体的xaml文件如下
<Window x:Class="metadataTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors"

Background="Chocolate" Title="元数据录入系统" Height="706" Width="1095" xmlns:my1="clr-namespace:FileSelectorControl;assembly=FileSelectorControl" xmlns:my2="clr-namespace:FileSelectorControl.Themes;assembly=FileSelectorControl">

<Grid Width="1009" Height="675">
<Menu Height="25" HorizontalAlignment="Left" Name="menu" VerticalAlignment="Top" Width="881" Margin="4,4,0,0">
<MenuItem Header="文件" Name="file" Margin="20,0,20,0">
<MenuItem Header="退出" Name="exit"></MenuItem>
</MenuItem>

<MenuItem Header="操作" Name="view" Margin="0,0,20,0"></MenuItem>
<MenuItem Header="帮助" Name="help" Margin="0,0,20,0"></MenuItem>
</Menu>

<my1:FileSelector Name="fileSelector1" Width="200" Height="300"/>

<StackPanel Name="inputArea" Margin="0,30,2,12" Background="Green">

</StackPanel>
</Grid>

</Window>



系统运行加载这个窗体,自定义控件FileSeletor不显示。这是问什么呢?
...全文
269 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lhx527099095 2012-10-25
  • 打赏
  • 举报
回复
实际上 就是需要给你的
<local:FileSelector Style="{StaticResource sssss}" x:Name="fileSelector1" Width="200" Height="300"/>
给个style 这个style在哪里定义 随便你 只要能取到 不报错应该就ok了吧
fengyoujie 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

我调试通过了啊 不过运行的时候点击添加的按钮会报错而已
代码就不贴了 改了几个地方
app.xaml里面添加了资源字典
<Application.Resources>
<ResourceDictionary Source="./Themes/Generic.xaml"/>
</Application.Resources>

给资源添加了key
<Sty……
[/Quote]


很感谢您的及时和热心的回复。您说在app.xaml增加一个资源字典,但是我创建的这个工程自定义控件工程没有app.xmal这个文件。
工程的目录FileSelectorControl(工程名)
FileSelectorControl
Properties
References
Themes
Generic.xaml
FileSelector.cs

lhx527099095 2012-10-24
  • 打赏
  • 举报
回复
我调试通过了啊 不过运行的时候点击添加的按钮会报错而已
代码就不贴了 改了几个地方
app.xaml里面添加了资源字典
<Application.Resources>
<ResourceDictionary Source="./Themes/Generic.xaml"/>
</Application.Resources>

给资源添加了key
<Style TargetType="{x:Type local:FileSelector}" x:Key="sssss">

调用的时候加了key
<local:FileSelector Style="{StaticResource sssss}" x:Name="fileSelector1" Width="200" Height="300"/>

你试试看看
fengyoujie 2012-10-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

调试了下 布局问题
把主窗体最外面的grid改成stackpanel就可以显示了
[/Quote]
谢谢,可是我把主窗体代码修改为如下

<Window x:Class="metadataTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:DevComponents.WpfEditors;assembly=DevComponents.WpfEditors"

Background="Chocolate" Title="元数据录入系统" Height="706" Width="1095" xmlns:my1="clr-namespace:FileSelectorControl;assembly=FileSelectorControl" xmlns:my2="clr-namespace:FileSelectorControl.Themes;assembly=FileSelectorControl">

<StackPanel Width="1009" Height="675">
<Menu Height="25" HorizontalAlignment="Left" Name="menu" VerticalAlignment="Top" Width="881" Margin="4,4,0,0">
<MenuItem Header="文件" Name="file" Margin="20,0,20,0">
<MenuItem Header="退出" Name="exit"></MenuItem>
</MenuItem>

<MenuItem Header="操作" Name="view" Margin="0,0,20,0"></MenuItem>
<MenuItem Header="帮助" Name="help" Margin="0,0,20,0"></MenuItem>
</Menu>



<StackPanel Name="inputArea" Margin="0,10,2,12" Background="Green">

</StackPanel>
<my1:FileSelector Name="fileSelector1" Height="177" />
</StackPanel>

</Window>



还是出不来,是什么原因呢?
lhx527099095 2012-10-24
  • 打赏
  • 举报
回复
调试了下 布局问题
把主窗体最外面的grid改成stackpanel就可以显示了
WPF Control Development Unleashed Building Advanced User Experiences In this book, two leading Windows Presentation Foundation experts give developers everything they need to build next-generation WPF applications–software that is more robust, usable, and compelling. Drawing on their close ties with Microsoft’s WPF development team, Pavan Podila and Kevin Hoffman give you a clear, robust, and practical understanding of WPF, its underpinnings, its overall architecture, and its design philosophy. Podila and Hoffman introduce never-before-published WPF design patterns and support them with robust, real-world code examples–all presented in full color, just as they appear in Visual Studio. The authors begin by explaining how to “think in WPF,” and then introduce powerful new techniques for everything from handling 3D layouts to creating game-like physics effects. Along the way, they offer in-depth coverage of data binding, building interactivity, and control development: three of WPF’s most challenging concepts. You’ll learn how to choose the right WPF features for every programming challenge, and use those features far more creatively and effectively. If you want to build truly outstanding WPF applications, this is the book that will get you there. Master the patterns and techniques you need to build state-of-the-art WPF applications Write more powerful and effective applications that reflect a deep understanding of WPF’s design philosophy Learn how WPF has evolved, and take full advantage of its growing sophistication Make the most of advanced declarative programming techniques Leverage IScrollInfo, virtualization, control theming, and other complex features Build more powerful interactivity into your WPF applications Create more visual software with 3D elements, custom animations, and shader effects Optimize WPF application performance in real-world environments Master design patterns for organizing your controls more effectively

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧