How to add a TextBox to TabItem? - c#

I'm trying to add a new TabItem to TabControl each time I click on a button and I have no problem with that. But I want a textbox inside each TabItem. How do I do that? I need to do that with code I suppose.
TabItem newTab = new TabItem();
newTab.Header = ncn.courseName;
newTab.FontSize = 20;
TextBox textbox = new TextBox();
textbox.Width = 200;
textbox.Height = 100;
textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
Grid grid = new Grid();
grid.Children.Add(textbox);
newTab.Content = grid;
this.Courses.Items.Add(newTab);
this.Courses.SelectedItem = newTab;

If you would like to use only code and not the MVVM pattern, this can be solved this way:
private void button1_Click(object sender, RoutedEventArgs e)
{
TabItem item = null;
Grid grid = null;
TextBox textbox = null;
try
{
// Creating the TextBox
textbox = new TextBox();
textbox.Width = 200;
textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
// Creating the Grid (create Canvas or StackPanel or other panel here)
grid = new Grid();
grid.Children.Add(textbox); // Add more controls
item = new TabItem();
item.Header = "Hello, this is the new tab item!";
item.Content = grid; // OR : Add a UserControl containing all controls you like, OR use a ContentTemplate
MyTabControl.Items.Add(item);
MyTabControl.SelectedItem = item; // Setting focus to the new TabItem
}
catch (Exception ex)
{
MessageBox.Show("Error creating the TabItem content! " + ex.Message);
}
finally
{
textbox = null;
grid = null;
item = null;
}
}
That is solving it "the old way" by using code-behind.
If you on the other want to use the WPF like it should, you can do like this.
To simplify a bit, I am using the code-behind as DataContext. I would recommend using a class instead in the running code.
I have also used the Cutton click event instead if using the Button Command.
First I create a "holder" class for the tab items, holding the data you need.
TabItemHolder.cs
public class TabItemHolder : DependencyObject, INotifyPropertyChanged
{
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
public String Header
{
get { return (String)GetValue(HeaderProperty); }
set
{
SetValue(HeaderProperty, value);
NotifyPropertyChanged("Header");
}
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
public String Text
{
get { return (String)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
NotifyPropertyChanged("Text");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
Then I have the model class, in this example the MainWindow.cs itself:
MainWindow.cs
public partial class MainWindow : Window, INotifyPropertyChanged
{
public static readonly DependencyProperty SelectedTabProperty = DependencyProperty.Register("SelectedTab", typeof(TabItemHolder), typeof(MainWindow), new UIPropertyMetadata());
public TabItemHolder SelectedTab
{
get { return (TabItemHolder)GetValue(SelectedTabProperty); }
set
{
SetValue(SelectedTabProperty, value);
NotifyPropertyChanged("SelectedTab");
}
}
public static readonly DependencyProperty TabsProperty = DependencyProperty.Register("Tabs", typeof(ObservableCollection<TabItemHolder>), typeof(MainWindow), new UIPropertyMetadata());
public ObservableCollection<TabItemHolder> Tabs
{
get { return (ObservableCollection<TabItemHolder>)GetValue(TabsProperty); }
set
{
SetValue(TabsProperty, value);
NotifyPropertyChanged("Tabs");
}
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.Tabs = new ObservableCollection<TabItemHolder>();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.Tabs.Add(new TabItemHolder() { Header = "Hello, this is the new tab item!", Text = "Dummy text for the textbox" });
this.SelectedTab = this.Tabs[this.Tabs.Count - 1];
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String PropertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
}
And finally, the XAML would be something like this.
MainWindow.xaml
<Grid x:Name="LayoutRoot">
<TabControl x:Name="MyTabControl"
Margin="12,67,12,12"
ItemsSource="{Binding Tabs}"
SelectedItem="{Binding SelectedTab}">
<TabControl.ContentTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Path=Text}"
Width="200"
HorizontalAlignment="Left"
VerticalAlignment="Top" />
</Grid>
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Header}"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
<Button Content="Button" Height="34" HorizontalAlignment="Left" Margin="19,12,0,0" Name="button1" VerticalAlignment="Top" Width="90" Click="button1_Click" />
</Grid>
That would do the same trick in a different (and in my opinion better) way.
I hope that helps you.

Related

How to update visibility at runtime in WPF

I am currently developing a hamburger style menu in WPF. In this menu, there are some categories that each have an icon. When the menu is collapsed you can still see those icons. When you expand the menu, there should appear text next to it. My idea was to just set their visibility to Visible as soon as the menu opens but I've had a lot of trouble realizing this. Right now I'm trying to change their visibility by binding them to a property.
XAML:
<ListView x:Name="menuItemsListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image x:Uid="Test" Name="InhoudImage" Source="Images/noimage.png" Height="30" Width="auto" VerticalAlignment="Center" Margin="3,0,0,0"></Image>
<TextBlock x:Uid="Test" Text="{Binding Path=TextboxVisibility}" Visibility="{Binding Path=TextboxVisibility}" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</ListViewItem>
</ListView>
C# CS Class:
using System.Windows;
using System.Windows.Controls;
namespace APP
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private bool menuOpen = false;
private int closedMenuWidth = 50;
private int openMenuWidth = 210;
private string textboxVisibility;
public string TextboxVisibility
{
get { return textboxVisibility; }
set { textboxVisibility = value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
this.TextboxVisibility = "Hidden";
}
private void MenuButton_Click(object sender, RoutedEventArgs e)
{
if (menuOpen)
{
menuGrid.Width = closedMenuWidth;
menuOpen = false;
this.TextboxVisibility = "Hidden";
}
else
{
menuGrid.Width = openMenuWidth;
menuOpen = true;
this.TextboxVisibility = "Visible";
//foreach (ListViewItem item in menuItemsListView.Items)
//{
// item.
// if (item.Uid == "Test")
// {
// item.Visibility = Visibility.Visible;
// }
//}
}
}
}
}
When I change the value within the MainWindow function, it does have an effect on it when it first starts. But the other times I try to change it, which is at runtime, nothing happens. I have tried all sorts of things with booleans and binding the actual Visibility type but nothing worked.
You should implemente INotifyPropertyChanged on your MainWindow class like this:
public partial class MainWindow: Window,INotifyPropertyChanged {
private string textboxVisibility;
public string TextboxVisibility {
get {
return textboxVisibility;
}
set {
textboxVisibility = value;
OnPropertyChanged();
}
}
//The rest of your code goes here
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
What OnPropertyChanged method does is, whenever the value is setted, it notifies the view and refreshes it.
This will solve the problem but isn't the right way to use MVVM.
The way you should do this is to change the visibility property of the TextBox instead of binding the visibility property to a value:
First you have to add a name to the TextBlock you want to hide:
<ListView x:Name="menuItemsListView" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListViewItem>
<StackPanel Orientation="Horizontal">
<Image x:Uid="Test" Name="InhoudImage" Source="Images/noimage.png" Height="30" Width="auto" VerticalAlignment="Center" Margin="3,0,0,0"></Image>
<TextBlock Name="textblock" x:Uid="Test" Text="{Binding Path=TextboxVisibility}" Visibility="{Binding Path=TextboxVisibility}" VerticalAlignment="Center"></TextBlock>
</StackPanel>
</ListViewItem>
</ListView>
And then you change the visibility in the code
private void MenuButton_Click(object sender, RoutedEventArgs e) {
if (menuOpen) {
menuGrid.Width = closedMenuWidth;
menuOpen = false;
textblock.Visibility = System.Windows.Visibility.Hidden;
}
else {
menuGrid.Width = openMenuWidth;
menuOpen = true;
textblock.Visibility = System.Windows.Visibility.Visible;
//foreach (ListViewItem item in menuItemsListView.Items)
//{
// item.
// if (item.Uid == "Test")
// {
// item.Visibility = Visibility.Visible;
// }
//}
}
}
If you want to implement MVVM the right way you have to create a ViewModel class and add it as Data Context to your view:
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
And then on you MainWindowViewModel is where you change the property:
public class MainWindowViewModel: INotifyPropertyChanged {
private string textboxVisibility;
public string TextboxVisibility {
get {
return textboxVisibility;
}
set {
textboxVisibility = value;
OnPropertyChanged();
}
}
//The rest of your code goes here
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged ? .Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}

modify the TextBlock value when click on Button of each ListViewItem

I have this code for my ListView:
<ListView x:Name="listme">
<ListView.ItemTemplate >
<DataTemplate >
<Grid>
...
<Button Background="{Binding ButtonColor}" x:Name="btnStar"
Click="btnStar_Click" Tag={Binding}>
<Image/>
<TextBlock Text="{Binding Path=all_like}" x:Name="liketext" />
</Button>
</Grid>
</DataTemplate >
</ListView.ItemTemplate >
</ListView >
I have 2 ListviewItems,each one has a "BtnStar" Button,each Button has a "liketext" TextBlock,one of those TextBlocks works only,per example when I click on btnStar of ListViewItem1 it modifies the TextBlock value of ListViewItem2's TextBlock,I can't modify the Text of TextBlock of ListViewItem1 when I click on BtnStar of ListViewItem1,this is my code:
ObservableCollection<Locals> Locals = new ObservableCollection<Locals>();
public async void getListePerSearch()
{
try
{
UriString2 = "URL";
var http = new HttpClient();
http.MaxResponseContentBufferSize = Int32.MaxValue;
var response = await http.GetStringAsync(UriString2);
var rootObject1 = JsonConvert.DeserializeObject<NvBarberry.Models.RootObject>(response);
foreach (var item in rootObject1.locals)
{
Item listItem = new Item();
if (listItem.all_like == null)
{
listItem.all_like = "0";
}
listme.ItemsSource = Locals;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var btn = sender as Button;
var item = btn.Tag as Locals;
item.all_like = liketext.Text;
liketext.Text = (int.Parse(item.all_like) + 1).ToString();
}
Locals.cs:
public class Locals : INotifyPropertyChanged
{
public int id_local { get; set; }
public string all_like { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
so,how can I modify the value of the TextBlock when I click on the BtnStar Button of each ListViewItem
thanks for help
Well. first of all you need use Binding approach in xaml apps.
your class Locals implement INotifyPropertyChanged but is bad implemented.
Please check this example:
public string someProperty {get;set;}
public string SomeProperty
{
get
{
return someProperty;
}
set
{
someProperty =value;
NotifyPropertyChanged("SomeProperty");
}
}
in your textblock you have Text={Binding SomeProperty}
you need to add Mode= TwoWay
Text={Binding SomeProperty, Mode= TwoWay}
finally in your click method
btnStar_Click
you need to do something like this:
var btn = sender as Button;
var local= btn.DataContext as Local;
local.SomeProperty= "my new value"
If you have implemented correctly INotifyPropertyChanged in your model you will see the change in your UI.
that's all.
Please mark this answer if it's useful for you!
Best Regards.

How to put a user control in a static layer ontop of all other controls?

I'm developing an autocomplete user control for WPF using XAML and C#.
I would like to have the pop-up for the suggestions to appear above all controls. Currently my pop up is a ListView . That causes problems since whenever I decide to show it the UI must find a place for it and to do so moves all the controls which are below it further down.
How can I avoid this? I assume I must put it in a layer which is above all of the other controls?
I have written "auto-complete" style controls before by using the WPF Popup control, combined with a textbox. If you use Popup it should appear, as you say, in a layer over the top of everything else. Just use Placement of Bottom to align it to the bottom of the textbox.
Here is an example that I wrote a while ago. Basically it is a text box which, as you type pops up a suggestions popup, and as you type more it refines the options down. You could fairly easily change it to support multi-word auto-complete style code editing situations if you wanted that:
XAML:
<Grid>
<TextBox x:Name="textBox"
Text="{Binding Text, Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IntelliSenseUserControl}}}"
KeyUp="textBox_KeyUp"/>
<Popup x:Name="popup"
Placement="Bottom"
PlacementTarget="{Binding ElementName=textBox}"
IsOpen="False"
Width="200"
Height="300">
<ListView x:Name="listView"
ItemsSource="{Binding FilteredItemsSource, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IntelliSenseUserControl}}}"
SelectionChanged="ListView_Selected"/>
</Popup>
</Grid>
Code-behind:
public partial class IntelliSenseUserControl : UserControl, INotifyPropertyChanged
{
public IntelliSenseUserControl()
{
InitializeComponent();
DependencyPropertyDescriptor prop = DependencyPropertyDescriptor.FromProperty(ItemsSourceProperty, typeof(IntelliSenseUserControl));
prop.AddValueChanged(this, ItemsSourceChanged);
}
private void ItemsSourceChanged(object sender, EventArgs e)
{
FilteredItemsSource = new ListCollectionView((IList)ItemsSource);
FilteredItemsSource.Filter = (arg) => { return arg == null || string.IsNullOrEmpty(textBox.Text) || arg.ToString().Contains(textBox.Text.Trim()); };
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(IntelliSenseUserControl), new FrameworkPropertyMetadata(null) { BindsTwoWayByDefault = true });
public object ItemsSource
{
get { return (object)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(object), typeof(IntelliSenseUserControl), new PropertyMetadata(null));
#region Notified Property - FilteredItemsSource (ListCollectionView)
public ListCollectionView FilteredItemsSource
{
get { return filteredItemsSource; }
set { filteredItemsSource = value; RaisePropertyChanged("FilteredItemsSource"); }
}
private ListCollectionView filteredItemsSource;
#endregion
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return || e.Key == Key.Enter)
{
popup.IsOpen = false;
}
else
{
popup.IsOpen = true;
FilteredItemsSource.Refresh();
}
}
private void UserControl_LostFocus(object sender, RoutedEventArgs e)
{
popup.IsOpen = false;
}
private void ListView_Selected(object sender, RoutedEventArgs e)
{
if (listView.SelectedItem != null)
{
Text = listView.SelectedItem.ToString().Trim();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
If your Window's content container is a Grid, you can simply do something like
<ListBox Grid.RowSpawn="99" Grid.ColumnSpan="99"/>
to "simulate" an absolute position. You then just have to set its position with Margin, HorizontalAlignment and VerticalAlignment so it lays around the desired control.

WPF, Property does not return value to the binding

So, I have a project with a scrolling text (marqee) that rotates over a string array. And I want it to change the string value after 20 seconds of each animation iteration.
There is a problem though, the property(ScrollingText) that uses the INotifyPropertyChanged interface to bind to a textblock(using XAML) does not return after the first iteration. Even though it refreshes normally(in the set part), it does not return on the Getter part.... except for the first set in the default ctor.
MAIN CLASS:
class GetScrollingText : CommonBase
{
private string _scrollingtext = String.Empty;
DoubleAnimation Animation;
public GetScrollingText()
{
ScrollingText = GetScrollString();
}
public string ScrollingText
{
get
{
return _scrollingtext;
}
set
{
if (value != _scrollingtext)
{
_scrollingtext = value;
RaisePropertyChanged("ScrollingText");
}
}
} // INJECTS the string in the animated textblock {binding}.
public TextBlock scrollBlock { get; set; }
string GetScrollString()
{
.........
return scrolltext;
}
public void LeftToRightMarqee(double from, double to)
{
Animation = new DoubleAnimation();
Animation.From = from;
Animation.To = to;
Animation.Duration = new Duration(TimeSpan.FromSeconds(20));
Animation.Completed += animation_Completed;
scrollBlock.BeginAnimation(Canvas.LeftProperty, Animation);
}
void animation_Completed(object sender, EventArgs e)
{
ScrollingText = GetScrollString();
scrollBlock.BeginAnimation(Canvas.LeftProperty, Animation);
}
}
For some reason the animation_Completed Event only changes the value ScrollingText, but it does not invoke the Getter part therefore there is not a return to the {binding}.
XAML:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:AnnouncingSys"
x:Class="AnnouncingSys.MainWindow"
x:Name="Window"
Width="1280" Height="720" MinHeight="566" MinWidth="710">
<Window.Resources>
<vm:GetScrollingText x:Key="ScrollingText"/>
</Window.Resources>
<Canvas x:Name="MainCanvas" ClipToBounds="True" Margin="0,0,0,0" Grid.Row="5" Background="Black" Grid.ColumnSpan="5" >
<TextBlock x:Name="ScrollBlock" TextWrapping="Wrap" VerticalAlignment="Top" Height="113" Width="5147" Canvas.Left="-1922" Text="{Binding ScrollingText, Source={StaticResource ScrollingText}}"/>
</Canvas>
</Window>
CODE BEHIND:
public partial class MainWindow : Window
{
GetScrollingText scrolling = new GetScrollingText();
public MainWindow()
{
InitializeComponent();
scrolling.scrollBlock = this.ScrollBlock;
scrolling.LeftToRightMarqee(2000, -3000);
}
}
And finally the helper class CommonBase:
public class CommonBase : INotifyPropertyChanged
{
protected CommonBase()
{
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string PropertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
PropertyChangedEventArgs e = new PropertyChangedEventArgs(PropertyName);
handler(this, e);
}
}
}
I have even put a breakpoint on the return block of the Getter but it only activates on the first: "ScrollingText = GetScrollString()". I mean, shouldn't it return each time the value is changed???
You are using two different instances of your GetScrollingText class, one in XAML as StaticResource, the other in code behind as the scrolling field in class MainWindow.
Instead of creating a StaticResource in XAML, you could just set the DataContext property of your MainWindow:
public partial class MainWindow : Window
{
GetScrollingText scrolling = new GetScrollingText();
public MainWindow()
{
InitializeComponent();
scrolling.scrollBlock = this.ScrollBlock;
scrolling.LeftToRightMarqee(2000, -3000);
DataContext = scrolling; // here
}
}
Now you would not explicitly set the binding's Source property, because the DataContext is used as default binding source:
<TextBlock ... Text="{Binding ScrollingText}"/>

How do I update a listview when my variable changes?

I have a conditional style using a StyleSelector, so that it changes the color of the playing song to green when the program loads. However, when the songIndex static variable is changed, I don't know how to make it update. I tried using the INotifyPropertyChanged interface, but am not sure how to use it properly or what I am supposed to bind to it. Here is my code....
public class HighlightStyleSelector : StyleSelector, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected override Style SelectStyleCore(object item, DependencyObject container)
{
//List<myMediaInterface> mediaList = new List<myMediaInterface>();
if (item == MainPage.mediaList[MainPage.songIndex])
{
Style style = new Style(typeof(ListViewItem));
style.Setters.Add(new Setter(ListViewItem.BackgroundProperty, new SolidColorBrush(Colors.LightGreen)));
return style;
}
else
{
var style = Application.Current.Resources["ListViewItemStyle1"] as Style;
return null;
}
}
public int songIndex
{
get { return MainPage.songIndex; }
set
{
songIndex = MainPage.songIndex;
OnPropertyChanged(songIndex.ToString());
}
}
protected void OnPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Xaml:
<ListView x:Name="songlistView" SelectionMode="Extended" DoubleTapped="songlistView_DoubleTapped" HorizontalContentAlignment="Stretch" BorderThickness="1" BorderBrush="#FF616161" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ManipulationMode="None" UseLayoutRounding="False" VerticalContentAlignment="Stretch" Margin="2,150,0,558" Tapped="songlistView_Tapped" FontSize="14" ItemContainerStyleSelector="{StaticResource HighlightStyleSelector}" ItemsSource="{Binding MainPage.mediaList}">
Here is the code for the custom listview
namespace HelloWorld
{
public class MyListView : Control
{
public int highlightedItem;
public MyListView()
{
this.DefaultStyleKey = typeof(MyListView);
}
}
}
If I use get; and set; for hightlighted item doesn't work either. Still says the member highlightedItem is not recognized or is not accessible
Edited 5/25
this is now in MainPage.xaml.cs
public int songIndex
{
get
{
return songIndex;
}
set
{
songIndex = value;
OnPropertyChanged("songIndex");
}
}
^^ not sure if this should go with my field declarations?
public void OnPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Here is my code from MainPage.xaml
<ListView x:Name="songlistView" SelectedIndex="{Binding songIndex}" SelectionMode="Extended" DoubleTapped="songlistView_DoubleTapped" HorizontalContentAlignment="Stretch" BorderThickness="1" BorderBrush="#FF616161" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollMode="Enabled" ManipulationMode="None" UseLayoutRounding="False" VerticalContentAlignment="Stretch" Margin="2,150,0,558" Tapped="songlistView_Tapped" FontSize="14" ItemsSource="{Binding MainPage.mediaList}"><!--ItemContainerStyleSelector="{StaticResource HighlightStyleSelector}"-->
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
Here is my code 5/26
I'm trying to create a dependencyproperty called highlightedIndex that is identical to selectedIndex, except that it is separate.
public class MyListView : ListView
{
public int highlightedIndex
{
get { return (int)GetValue(HighlightedProperty); }
set
{
SetValue(HighlightedProperty, value);
}
}
public static readonly DependencyProperty HighlightedProperty = DependencyProperty.Register("HighlightedProperty", typeof(int), typeof(MyListView), new PropertyMetadata(0));
}
namespace HelloWorld
{
public class HighlightStyleSelector : StyleSelector
{
protected override Style SelectStyleCore(object item, DependencyObject container)
{
if (item == MainPage.mediaList[MainPage.songIndex])
{
var style = Application.Current.Resources["ListViewItemHighlighted"] as Style;
Setter setter = new Setter(ListViewItem.BackgroundProperty, new SolidColorBrush(Colors.LightGreen));
//Style style = new Style(typeof(ListViewItem));
style.Setters.Add(setter);
return style;
}
else
{
var style = Application.Current.Resources["ListViewItemStyle1"] as Style;
return style;
}
}
}
}
I'm a bit confused because of static properties on MainPage you seem to be assigning songIndex to and binding mediaList to. It would be helpful to see that code as well.
Still, you need to fix your property (assuming OnPropertyChanged is implemented correctly):
public int songIndex
{
get { return MainPage.songIndex; }
set
{
// set the assigned value to property backing field
MainPage.songIndex = value;
// you need to notify with the name of the property as the argument
OnPropertyChanged("songIndex");
}
}
Then you can bind to this property like any other with the only difference that the control will be notified when its value changes:
<ListView SelectedIndex="{Binding songIndex}" />
public static readonly DependencyProperty HighlightedProperty = DependencyProperty.Register("highlightedIndex", typeof(int), typeof(MyListView), new PropertyMetadata(null, propertyChanged));
private static void propertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
int newValue = (int)e.NewValue;
ListView lv = (ListView)d;
foreach (ListViewItem lvi in lv.Items)
{
if (lv.Items.IndexOf(lvi) == newValue)
{
lvi.Background = new SolidColorBrush(Colors.LightGreen);
}
else
{
lvi.Background = new SolidColorBrush();
}
}
}
Didn't need the styleselector or any binding

Categories