How to programmatically change selected tabItem Header colour from code behind - c#

I have a program in which I have to load colours at startup.
Then I have to add a tabcontrol with tab items:
TabControl tabc = m_Executer.GetTabControl();<---- from outside
if (tabc != null)
{
TabItem tbi = new TabItem();
tbi.Header.Background = Brushes.Transparent;
tbi.Header.Content = "End manager";
tabc.Items.Add(tbi);
tabc.SelectionChanged += (sender2, args) =>
{
((TabItem)(tabc.SelectedItem)).Background = Brushes.Red;
tabc.UpdateLayout();
};
}
the idea is changing the background when the selectionchanged event is fired but that doesn't work.

The default control template for a TabItem contains two Border elements named "mainBorder" and an "innerBorder". You could change the Background of the latter one:
tabc.SelectionChanged += (sender2, args) =>
{
if(args.AddedItems != null && args.AddedItems.Count > 0)
{
TabItem ti = args.AddedItems[0] as TabItem;
if(ti != null && VisualTreeHelper.GetChildrenCount(ti) > 0)
{
Grid grid = VisualTreeHelper.GetChild(ti, 0) as Grid;
if (grid != null)
{
Border mainBorder = grid.Children[0] as Border;
if (mainBorder != null)
{
Border innerBorder = mainBorder.Child as Border;
if(innerBorder != null)
innerBorder.Background = Brushes.Red;
}
}
}
}
};
The above code works on Windows 10. The templates may differ between different versions of Windows.
If you want to change the background of the unselected TabItem you simply replace AddedItems with RemovedItems and set the Background property of the mainBorder.

You might use a textblock to change color:
TabItem tbi = new TabItem();
tbi.Header = Brushes.Transparent;
TextBlock tbk = new TextBlock() { Text="End manager" };
tbi.Header = tbk;
tabc.Items.Add(tbi);
and then change the textblock on the event
tabc.SelectionChanged += (sender2, args) =>
{
((TabItem)(tabc.SelectedItem)).Foreground = Brushes.Red;
tabc.UpdateLayout();
};
EDIT you'd better de-highlight the unselected items:
tabc.SelectionChanged += (sender2, args) =>
{
foreach (var item in tabc.Items)
{
if (item == ((TabItem)(tabc.SelectedItem)))
((TabItem)item).Foreground = Brushes.Red;
else
((TabItem)item).Foreground = Brushes.Black;
}
};

You have to set the Content to TextBlock instead of a string:
XAML:
<TabControl>
<TabControl.Items>
<TabItem >
<TabItem.Header>
<TextBlock Background="Red" Text="Foo"/>
</TabItem.Header>
</TabItem>
<TabItem Header="Bar" />
</TabControl.Items>
</TabControl>
or code behind (for whatever reason...)
TabItem tbi = new TabItem();
TextBlock headerElement = new TextBlock();
headerElement.Text = "End manager";
headerElement.Background = Brushes.Red;
tbi.Header = headerElement;
I also suggest you do take a deep dive into the MVVM pattern. In 98% of the cases it's best practice to write your UIs in XAML and bind to a ViewModel.
BTW: Your code doesn't even compile as Headeris of type object...

Related

Is there any way to create new tabItem in TabControl WPF?

I made a code in WinForm. It works.
I dragged xml file and dropped to tabPage.
private TextEditorControl AddNewTextEditor(string title)
{
var tab = new TabPage(title);
var editor = new TextEditorControl();
editor.Dock = System.Windows.Forms.DockStyle.Fill;
editor.IsReadOnly = false;
editor.Document.DocumentChanged +=
new DocumentEventHandler((sender, e) => { SetModifiedFlag(editor, true); });
// When a tab page gets the focus, move the focus to the editor control
// instead when it gets the Enter (focus) event. I use BeginInvoke
// because changing the focus directly in the Enter handler doesn't
// work.
tab.Enter +=
new EventHandler((sender, e) => {
var page = ((TabPage)sender);
page.BeginInvoke(new Action<TabPage>(p => p.Controls[0].Focus()), page);
});
tab.Controls.Add(editor);
fileTabs.Controls.Add(tab);
if (_editorSettings == null) {
_editorSettings = editor.TextEditorProperties;
OnSettingsChanged();
} else
editor.TextEditorProperties = _editorSettings;
return editor;
}
But WPF is a bit dirrenet.
Can I change the code for WPF?? or other way..? Thanks for your help.
You can do it this way:
Suppose you have this TabControl with TabItem A and B and a Button for adding TabItem
<StackPanel>
<TabControl x:Name="TabControl">
<TabItem Header="A"/>
<TabItem Header="B"/>
</TabControl>
<Button Content="Add New Tab Item" Click="ButtonBase_OnClick"/>
</StackPanel>
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var tabItem = new TabItem { Header = "C" };
TabControl.Items.Add(tabItem);
}
After clicking the button you will be added another TabItem (C)

WPF C# Get text from a Textblock inside a ListBoxItem

How can i access a Textblock inside a Stackpanel that is inside a ListBoxItem?
For example:
ListBoxItem MyItem = new ListBoxItem();
StackPanel StackPnl = new StackPanel();
TextBlock Title = new TextBlock();
Title.Text = "Item 1";
StackPnl.Children.Add(Title);
MyItem.Content = StackPnl;
How can i later access the Text property of that Textblock with Listbox.SelectedItem?
Try this:
//listBox1 is your ListBox
ListBoxItem MyItem = listBox1.SelectedItem as ListBoxItem;
if(MyItem != null)
{
StackPanel sp = MyItem.Content as StackPanel;
if(sp != null && sp.Children.Count > 0)
{
TextBlock textBlock = sp.Children[0] as TextBlock;
if(textBlock != null)
{
string text = textBlock.Text;
}
}
}

c# wpf - ListView.ScrollIntoView(LastItem) does not work properly

like some other people here i have a ListView (updated via binding in a GridView).
I want to keep the last inserted Item in the View. So i tried
LView.ScrollIntoView(LView.Items[LView.Items.Count - 1]);
This is working almost fine. Altough the first item which would have to be scrolled into view is only shown like 80% of its whole row (depending on how high i define the whole ListView, i almost got 100%).
The real problem is that the following items which should get scrolled into view are not shown. It is also noticable at the Scrollbar itself which is not at the bottom.
Last Item is not shown
Here is the code of my MainWindow.
public partial class MainWindow : Window
{
private InterfaceCtrl ICtrl;
private ListView LView;
public MainWindow()
{
InitializeComponent();
this.ICtrl = new InterfaceCtrl();
this.ICtrl.ProgressCollection.CollectionChanged += this.CollectionChanged;
Grid MainGrid = new Grid();
this.Content = MainGrid;
GridView gv = new GridView();
Binding StartTimeStampBinding = new Binding() { Path = new PropertyPath("StartTS"), Mode = BindingMode.OneWay, StringFormat = "dd.MM.yyyy - HH:mm:ss.fff" };
GridViewColumn gvTCStartTS = new GridViewColumn() { Header = "Time", Width = 150.00, DisplayMemberBinding = StartTimeStampBinding };
gv.Columns.Add(gvTCStartTS);
LView = new ListView() { Height = 192, Width = 250, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, View = gv, ItemsSource = this.ICtrl.ProgressCollection };
MainGrid.Children.Add(LView);
ICtrl.StartMyThread();
}
private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new System.Action(delegate ()
{
if (LView != null && LView.Items.Count > 0)
{
LView.UpdateLayout();
//LView.ScrollIntoView(LView.Items[LView.Items.Count - 1]);
LView.SelectedIndex = LView.Items.Count;
LView.ScrollIntoView(LView.SelectedItem);
}
}));
}
}
Thank you.
EDIT:
It seemed to be a timing problem, although all the wanted data was in the LView at the right time i tried a workaround with a Textbox bound to the Timestamp.
TextBox tb = new TextBox(); // { Width = 250, Height = 28, Margin= new Thickness(10,100,1,0)};
tb.SetBinding( TextBox.TextProperty , new Binding("LastMsgTimestamp") { Source = this.ICtrl, Mode = BindingMode.OneWay, StringFormat = "dd.MM.yyyy - HH:mm:ss.fff" });
tb.TextChanged += this.UpdateScrollbar;
tb.Visibility = Visibility.Hidden;
It seems to me like there is a timing issue within the binding to the LView and the fired Event of the ObservableCollection. This also includes the PropertyChanged of the ObservableCollection.
I tried the events TargetUpdated and SoruceUpdated directly within LView but those didn't came up at all.
You could try to call any of the ScrollToBottom() or ScrollToVerticalOffset() methods of the ListView's internal ScrollViewer element:
private void CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new System.Action(delegate ()
{
if (LView != null && LView.Items.Count > 0)
{
LView.UpdateLayout();
ScrollViewer sv = GetChildOfType<ScrollViewer>(LView);
if (sv != null)
sv.ScrollToBottom();
LView.SelectedIndex = LView.Items.Count;
LView.ScrollIntoView(LView.SelectedItem);
}
}));
}
private static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj == null)
return null;
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
var child = VisualTreeHelper.GetChild(depObj, i);
var result = (child as T) ?? GetChildOfType<T>(child);
if (result != null)
return result;
}
return null;
}
I have made the following sample. You could try to call ScrollToBottom in inner ScrollViewer as #mm8 points out. Nevertheless when saw the answer I was already making my sample, so here it is:
Codebehind
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace ListViewScroll
{
public partial class MainWindow : Window
{
public ObservableCollection<string> Names { get; set; }
public MainWindow()
{
InitializeComponent();
Names = new ObservableCollection<string>();
ListView.ItemsSource = Names;
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
Names.Add("Some Name" + ++someInt);
// Get the border of the listview (first child of a listview)
var border = VisualTreeHelper.GetChild(ListView, 0) as Decorator;
// Get scrollviewer
var scrollViewer = border.Child as ScrollViewer;
scrollViewer.ScrollToBottom();
}
private static int someInt;
}
}
XAML
<Window x:Class="ListViewScroll.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">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<ListView Grid.Row="0" Name="ListView"/>
<Button Content="Add" FontSize="20" Grid.Row="1"
Click="ButtonBase_OnClick"/>
</Grid>
</Window>
In this case I am handling the scrolling in the button click event but you may change this to fit your requirements
It works, I have tested.
Hope this helps

Unable to change back color of selected tabitem in silverlight

I have a TabControl with TabItem. I want to change the background colour of the tabitem header for the selected tab.
So I set the XAML code as below
<sdk:TabControl Background="WhiteSmoke" Foreground="Black"
SelectionChanged="TabControl_SelectionChanged">
<sdk:TabItem Name="adminTab" BorderBrush="Black">
<sdk:TabItem.Header>
<StackPanel Name="adminsp" Background="#C7CEF7">
<Image Name="ico1" Source="Images/admin.png"/>
<TextBlock Text="Admin"/>
</StackPanel>
</sdk:TabItem.Header>
</sdk:TabItem>
<sdk:TabItem Name="userTab" BorderBrush="Black">
<sdk:TabItem.Header>
<StackPanel Name="usersp" Background="#C7CEF7">
<Image Name="ico1" Source="Images/user.png"/>
<TextBlock Text="User"/>
</StackPanel>
</sdk:TabItem.Header>
</sdk:TabItem>
and in CS code as
void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tab = sender as TabControl;
if (adminTab.IsSelected)
{
adminsp.Background = new SolidColorBrush(Colors.Blue);
}
else
{
adminsp.Background = new SolidColorBrush(Color.FromArgb(255, 199, 229, 249));
}
.
.
}
But the background color is not changing, Any help would be appreciated!
You should do something like this if you want to do it directway, otherwise you should Edit the style of TabControl,
TabControl currentTab = (TabControl)sender;
TabItem selectedItem = currentTab.SelectedItem as TabItem;
if (selectedItem != null)
{
foreach (TabItem currentItem in currentTab.Items)
{
if (currentItem == selectedItem)
{
selectedItem.BorderBrush = new SolidColorBrush() { Color = Colors.Green };
selectedItem.Background = new SolidColorBrush() { Color = Colors.LightGray };
}
else
{
currentItem.BorderBrush = new SolidColorBrush() { Color = Colors.Blue };
currentItem.Background = new SolidColorBrush() { Color = Colors.Gray };
}
}
}

How to highlight a LongListSelector Item on the UI programmatically

I need to highlight and manipulate an item of LongListSelector on the UI for the user.
I see (this) example on code samples but couldn't understand it well.
How can I change background of the inner StackPanel which belongs to the SelectedItem and add a TextBlock into it, programmatically in code behind?
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
To make the sample you linked work with a StackPanel
private void lls_SelectionChanged(object sender, SelectionChangedEventArgs e) {
var spList = new List<StackPanel>();
GetItemsRecursive<StackPanel>(lls, ref spList);
// Selected.
if (e.AddedItems.Count > 0 && e.AddedItems[0] != null) {
foreach (var sp in spList) {
if (e.AddedItems[0].Equals(sp.DataContext)) {
sp.Background = new SolidColorBrush(Colors.Green);
sp.Children.Add(new TextBlock { Text = "Hello" });
}
}
}
// Unselected.
if (e.RemovedItems.Count > 0 && e.RemovedItems[0] != null) {
foreach (var sp in spList) {
if (e.RemovedItems[0].Equals(sp.DataContext)) {
sp.Background = (SolidColorBrush)Resources["PhoneBackgroundBrush"];
sp.Children.RemoveAt(sp.Children.Count - 1);
}
}
}
}
I am not sure whether I understood your question correctly since I am new in windows phone development. Here is the code for changing the background of stackpanel and adding textblock to it programmatically.
enter code here
// Constructor
public MainPage()
{
InitializeComponent();
// change the background of stackpanel
StackPanel st = new StackPanel();
SolidColorBrush mysolidbrush = new SolidColorBrush();
mysolidbrush.Color = Color.FromArgb(255, 100,100,10); // RGB color
st.Background = mysolidbrush;
// Adding textblock to the stackpanel
TextBlock txtblk = new TextBlock();
st.Children.Add(txtblk);
}
Best,
B

Categories