How to Bind Tab Items to the TabControl Dynamically and Display a RichTextBox in each tab and i want to append some data to the RichTextBox in MVVM Format.
Asked
Active
Viewed 639 times
0
-
WPF or UWP or Xamarin Forms? – Liero May 16 '17 at 18:06
1 Answers
1
Well... here is some generalized code which you needed. If you have any sppecific requirement let me know ...
XAML....
<Window x:Class="wpfApplication.Views.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:GateApplication.Views"
xmlns:c="clr-namespace:wpfApplication.Commands"
xmlns:vm="clr-namespace:wpfApplication.ViewModels"
mc:Ignorable="d"
Title ="Test" Height="550" Width="825">
<Window.DataContext>
<vm:TestVM></vm:TestVM>
</Window.DataContext>
<Grid>
<TabControl Width="450" Margin="2,2,2,2" ItemsSource="{Binding Tabs}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"></TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate>
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run Text="{Binding Content}"></Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Grid>
</Window>
and View Model is...
public class TestVM : ViewModelBase
{
public TestVM()
{
Tabs = new ObservableCollection<TabItem>();
Tabs.Add(new TabItem { Header = "One", Content = "One's content" });
Tabs.Add(new TabItem { Header = "Two", Content = "Two's content" });
}
public sealed class TabItem
{
public string Header { get; set; }
public string Content { get; set; }
}
}

Lina
- 163
- 13