如何在C#中写入Dock内容

时间:2013-02-22 20:44:27

标签: c# dock dockpanel

有一个DockPanel,当我运用这个代码时,它会将停靠内容添加到正确的Dock但是我无法让它显示文本(即执行Step1和Step2等,如下所示)。我做了很多研究,没有任何效果。在此先感谢您的帮助。

public void ShowInstructionForm()
{
    dragDropForm = new DockContent();
    dragDropForm.Name = "Hints";

    dragDropForm.TabText = "Hints2";
    dragDropForm.ShowHint = DockState.DockRight;
    dragDropForm.BackColor = Color.White;
    dragDropForm.Text = "- Perform the step number 1 ."
        + Environment.NewLine + " - Perform the Step number 2";                                     

    try
    {
        dragDropForm.Show(this.oDock.MainDock);
    }
    catch (Exception e)
    {
        MessageBox.Show(this.oDock.MainDock, "error happened  " + e.Message);
    }
}

2 个答案:

答案 0 :(得分:1)

就个人而言,我会使用数据绑定来实现你想要的东西,这样你就会坚持更严格的设计模式。

<强> XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <DockPanel>
        <TextBlock Text="{Binding Path=Foo}" />
    </DockPanel>
</Window>

<强> C#

public partial class MainWindow : Window
{
    public string Foo { get; set; }

    public MainWindow()
    {
        Foo = "hello world"; // Changing Foo 'automagically' changes your textblock value
        InitializeComponent();
    }
}

通过将业务逻辑与UI代码分开,可以使您更加灵活。显然,这只是一个与Dock面板中的文本块绑定数据的示例,但希望这可以让您更好地理解。

答案 1 :(得分:0)

你可以进入xaml并在DockPanel中放置一个文本块,如下所示:

<DockPanel>
  <TextBlock Text="- Perform the step number 1 ."/>
</DockPanel>
相关问题