简体   繁体   English

将控件添加到用户控件

[英]Add Control to User Control

I created a user control with WPF which is supposed to take in a list of controls as its content.我用 WPF 创建了一个用户控件,它应该将控件列表作为其内容。 I succeeded in creating such a user control, but have run into the issue that I cannot name any of the sub-controls I added.我成功创建了这样的用户控件,但遇到了无法命名添加的任何子控件的问题。

Here's the code for the user control:这是用户控件的代码:

public static DependencyProperty InnerContentProperty = DependencyProperty.Register("TaskListItems", 
typeof(UIElement), typeof(TaskList));

public UIElement TaskListItems {
    get { return (UIElement)GetValue(InnerContentProperty); }
    set { SetValue(InnerContentProperty, value); }
}

And the XAML (relevant part):和 XAML(相关部分):

<ScrollViewer>
    <ContentControl Content="{Binding TaskListItems, RelativeSource={RelativeSource AncestorType={x:Type local:TaskList}}}"/>
</ScrollViewer>

The implementation then looks like so:实现看起来像这样:

<uc:TaskList Grid.Column="0" HeaderText="Daily Task List" FooterText="Completed: 10">
    <uc:TaskList.TaskListItems>
        <StackPanel>
            <Button Content="Some Button"/>
            <Button Content="Some Button 2"/>
         </StackPanel>
    </uc:TaskList.TaskListItems>
</uc:TaskList>

So when I add a control to my user control such as a button for example, everything works fine, except I cannot give the button a name and I get the error:因此,当我向我的用户控件添加控件(例如按钮)时,一切正常,除了我无法为按钮命名并且出现错误:

Cannot set Name attribute value 'test' on element 'Button'. 'Button' is under the scope of element 'TaskList', which already had a name registered when it was defined in another scope

So my question is, is there a way to functionally do the same thing here, but also letting me give my controls names?所以我的问题是,有没有办法在这里在功能上做同样的事情,但也让我给我的控件命名?

Edit for more information:编辑以获取更多信息:

任务清单设计

任务列表项设计

What I'm going for is a user control that can take in a list of other user controls I created.我要的是一个用户控件,它可以接收我创建的其他用户控件的列表。
Those "list item" controls have four buttons each (the text bit is also a button):这些“列表项”控件每个都有四个按钮(文本位也是一个按钮):

The text button just opens a window with info about the selected task.文本按钮只是打开一个窗口,其中包含有关所选任务的信息。
The check mark button is supposed to create an event to request the tasks completion.复选标记按钮应该创建一个事件来请求任务完成。
The vertical ellipsis button opens a window where you can modify the task.垂直省略号按钮打开一个窗口,您可以在其中修改任务。
Lastly the x button is supposed to create an event to request cancellation/removal of the selected task.最后 x 按钮应该创建一个事件来请求取消/删除所选任务。

Issue lies with creating the events and subscribing to them .问题在于创建事件并订阅它们 What I have so far are two event handlers which are invoked in each of the buttons click events.到目前为止,我有两个事件处理程序,它们在每个按钮单击事件中调用。

public event EventHandler<TaskItemEventArgs> TaskCompletedEvent;
public event EventHandler<TaskItemEventArgs> TaskRemoveRequestEvent;

private void acceptBtn_click(object sender, RoutedEventArgs e)
{
    TaskCompletedEvent?.Invoke(this, new TaskItemEventArgs() { EventText = "Task Completed!" });
}

private void removeBtn_click(object sender, RoutedEventArgs e)
{
    TaskRemoveRequestEvent?.Invoke(this, new TaskItemEventArgs() { EventText = "Task Remove Requested!" });
}

The problem now is that I don't know where to subscribe these events to in order to modify the "task item" control that invoked the event.现在的问题是我不知道在哪里订阅这些事件以修改调用该事件的“任务项”控件。 I was thinking I should simply add it in the parent control, but I wouldn't know how to go about that, as all the controls here are dynamic.我想我应该简单地将它添加到父控件中,但我不知道如何去做,因为这里的所有控件都是动态的。

Now that I think about it, I guess you wouldn't need a control name for the above task.现在我考虑一下,我想您不需要上述任务的控件名称。 The dynamic controls have me confused though.不过,动态控件让我感到困惑。

I actually got the code to work now after a bit of experimentation and googling.在经过一些实验和谷歌搜索之后,我实际上让代码现在可以工作了。 I dynamically create a stack panel in the TaskList control and when I hit the "plus" button in the top right corner, a dialog opens allowing me to describe my task.我在 TaskList 控件中动态创建了一个堆栈面板,当我点击右上角的“加号”按钮时,会打开一个对话框,允许我描述我的任务。 After that I dynamically create a TaskListItem and subscribe to it's events with some methods I created in TaskList.之后,我动态创建一个 TaskListItem 并使用我在 TaskList 中创建的一些方法订阅它的事件。 Finally I add the TaskListItem to the StackPanel and if I want to modify it I can just modify the specifc object that asked to be edited.最后,我将 TaskListItem 添加到 StackPanel,如果我想修改它,我可以修改要求编辑的特定对象。 Works pretty well.工作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM