在WPF中动态创建用户控件实例 [英] Dynamically Creating an Instance of User Control in WPF
问题描述
嗨朋友们,
你们好吗?实际上我陷入了困境,现在我真的需要一个快速的帮助。我是WPF的初学者,在创建项目的过程中我疯了。我正在尝试创建一个可用于与我的硬件接口的软件。现在我正在显示硬件的输出,它是串行接收到软件中的,使用定制的控制,看起来更像一个小灯。当我将它的'Glow布尔属性设置为true时,它会亮起。顺便说一句,我宁愿谈论这个问题。我想创建这个用户控件的''n'个实例,其中''n''是我在运行时在文本框中手动输入的值,我只知道我在WinForms中使用的技术。但是对于WPF,它不起作用。
我有:
主窗口
>网格
>扩展器
>网格
> WrapPanel
现在我需要在WrapPanel中动态创建用户控件的实例。你可以告诉我如何进步,因为我几乎到处搜索,但没有得到任何满意的答案。我将非常感谢你。
问候
Tushar Srivastava
(顺便说一句,对不起,如果我用得太坦率了一种语言)
你的XAML的骨架:
< 窗口 >
< 网格 >
< 扩展器 >
< 网格 >
< span class =code-keyword>< WrapPanel 名称 = myAwesomeNameForAWrapPanel >
< / WrapPanel >
< / Grid >
< / Expander >
< / Grid >
< / Window >
在你的代码隐藏中:
for ( int i = 0 ; i < 10 ; i ++)
{
MyUserControl uc = new MyUserControl();
myAwesomeNameForAWrapPanel.Children.Add(uc);
}
您要查找的内容是模板和列表控件之一(例如ListBox)。你要做的是创建一个代表硬件状态的类,然后有一个包含这些硬件状态类集合的类。这样的事情:public class HardwareState:INotifyPropertyChanged
{
private bool lightOn;
public bool LightOn
{
获取 {返回 lightOn; }
set
{
if (lightOn!= value )
{
lightOn = value ;
RaiseNotification( LightOn);
}
}
}
public event PropertyChangedEventHandler的PropertyChanged;
private void RaiseNotification( string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!= null )
{
handler( this , new PropertyChangedEventArgs(propertyName));
}
}
}
public class HardwareStateManager {
// 这是您绑定的类 - 使用HardwareState值填充State 。
public ObservableCollection< HardwareState>状态{获取; set ; }
}现在,您只需要在视图中绑定到此类:
< Window.DataContext >
< vm:HardwareStateManager / >
< / Window.DataContext >
< Window.Resources >
< DataTemplate x:Key = LightsTemplate >
< uc:MyLightControl / >
< / DataTemplate >
< 样式 TargetType = {x:Type ListBox} >
< Setter 属性 = ScrollViewer.HorizontalScrollBarVisibility 值 = 自动 / >
< Setter Property = ScrollViewer.VerticalScrollBarVisibility 值 = 自动 < span class =code-attribute> / >
< Setter 属性 = ScrollViewer.CanContentScroll 值 = true / >
< Setter 属性 = 模板 >
< < span class =code-leadattribute> Setter.Value >
< ControlTemplate TargetType = {x:类型ListBox} >
< ScrollViewer 方向 = 水平 >
< ; ItemsPresenter / < span class =code-keyword>>
< / ScrollViewer >
< / ControlTemplate >
< / Setter.Value >
< / Setter >
< / Style >
< / Window.Resources >
< 网格 >
< ListBox ItemsSource = {Binding State} ItemsTemplate = {StaticResource LightsTemplate} / >
< / Grid > $ p $注意 - 我在这里错过了vm和uc命名空间的定义 - 你会在窗口声明中将它们添加为xaml:vm =clr-namespace:....引用。
最后,我只是在CP编辑器窗口中键入了这个 - 其中一些可能在样式中100%正确,因为我只是从内存中执行此操作,但它应该让你入门。
Hi Friends,
How are you all? Actually I am stuck at a point and Now I really need a quick help. I am completely beginner to WPF and I have gone wild while creating a project in it. I am trying to create a software that I can use to interface with one of my hardware. Now I am displaying the output of the hardware, that is received serially into the software, using a custom made control which looks more like a small light. It lights on when I set it''s "Glow" boolean Property to true. By the way I would rather talk about the problem. I want to create ''n'' instances of this user control where ''n'' is the value that I will enter manually in a textbox during runtime and I only know the technique that I had used with WinForms. But with WPF, it''s not working.
I have :
Main Window > Grid > Expander > Grid > WrapPanel
Now I need to dynamically create the instances of my User Control in WrapPanel. Can you provide me with some idea of how to progress as I am searching almost everywhere but didn''t get any satisfactory answer. I will be very thankful to you.
Regards
Tushar Srivastava
(By the way, Sorry if I used too frank a language)
Skeleton of your XAML:
<Window> <Grid> <Expander> <Grid> <WrapPanel Name="myAwesomeNameForAWrapPanel"> </WrapPanel> </Grid> </Expander> </Grid> </Window>
In your code-behind:
for(int i = 0; i < 10; i++) { MyUserControl uc = new MyUserControl(); myAwesomeNameForAWrapPanel.Children.Add(uc); }
The things you are looking for are templates and one of the list controls (such as a ListBox). What you would do is create a class that represents the state of the hardware, and then have a class that holds a collection of these hardware state classes. Something like this:public class HardwareState : INotifyPropertyChanged { private bool lightOn; public bool LightOn { get { return lightOn; } set { if (lightOn != value) { lightOn = value; RaiseNotification("LightOn"); } } } public event PropertyChangedEventHandler PropertyChanged; private void RaiseNotification(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } public class HardwareStateManager { // This is the class that you bind to - populate State with the HardwareState values. public ObservableCollection<HardwareState> State { get; set; } }Now, you simply need to bind to this class in your view:
<Window.DataContext> <vm:HardwareStateManager /> </Window.DataContext> <Window.Resources> <DataTemplate x:Key="LightsTemplate"> <uc:MyLightControl /> </DataTemplate> <Style TargetType="{x:Type ListBox}"> <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" /> <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" /> <Setter Property="ScrollViewer.CanContentScroll" Value="true" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListBox}"> <ScrollViewer Orientation="Horizontal"> <ItemsPresenter /> </ScrollViewer> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <ListBox ItemsSource="{Binding State}" ItemsTemplate="{StaticResource LightsTemplate}" /> </Grid>Note - I''ve missed out the definition of the vm and uc namespace here - you would add these as xaml:vm="clr-namespace:...." references in the window declaration.
Finally, I just typed this out in the CP editor window - some of this might be 100% correct in the styling, as I''m just doing this from memory, but it should get you started.
这篇关于在WPF中动态创建用户控件实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!