Background / Foreground Capability in Custom ListView Items


Background / Foreground Capability in Custom ListView Items



I have the following functioning code that binds GridViewColumns to data from a custom class:


GridViewColumns


<ListView Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Header="First" DisplayMemberBinding="{Binding lvi.firstName}"/>
<GridViewColumn Header="Last" DisplayMemberBinding="{Binding lvi.lastName}"/>
</GridView>
</ListView.View>
</ListView>




public class LVItemBox {
public LVItem lvi { get; set; }
}
public class LVItem : INotifyPropertyChanged {
private string _firstName;
private string _lastName;
public string firstName {
get { return _firstName; }
set { SetField(ref _firstName, value); }
}
public string lastName {
get { return _lastName; }
set { SetField(ref _lastName, value); }
}
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged(string propertyName) {PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
public bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null) { if (EqualityComparer<T>.Default.Equals(field, value)) return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
LVItem lvi1 = new LVItem {firstName = "John", lastName = "Doe"};
LVItem lvi2 = new LVItem {firstName = "Jane", lastName = "Smith"};
lv.Items.Add(new LVItemBox {lvi = lvi1});
lv.Items.Add(new LVItemBox {lvi = lvi2});
}
}



My dilemma is that I want background / foreground Brush capability within LVItemBox, however if I make LVItemBox extend Control, changing Background/Foreground has no effect:


Brush


LVItemBox


LVItemBox


Control


Background


Foreground


public class LVItemBox : Control {
public LVItem lvi { get; set; } // data displays
}
...
...
private void changeBackground(object sender, EventArgs e) {
LVItemBox lvib = (LVItemBox)lv.Items[0];
lvib.Background = Brushes.Black; // doesn't work
}



Furthermore, if I extend ListViewItem instead of Control I can get the background change to work, but the data bindings no longer work.


ListViewItem


Control


public class LVItemBox : ListViewItem {
public LVItem lvi { get; set; } // data doesn't display
}
...
...
private void changeBackground(object sender, EventArgs e) {
LVItemBox lvib = (LVItemBox)lv.Items[0];
lvib.Background = Brushes.Black; // works
}



Any idea how I can get foreground / background capability within LVItemBox?


LVItemBox





Instead of DisplayMemberBinding, you could set the CellTemplate of the GridViewColumns to a DataTemplate that contains appropriate UI elements which bind to the foreground and background properties of the item class.
– Clemens
Jul 3 at 5:44






When you extend Control I can't see any INPC being triggered in the setter. besides, using Control would break MvvM, also you haven't shown us how you bind that foreground or background in XAML. @Clemens is correct here, you should have used DataTemplate. And look into Styles in WPF, makes the structure a bit more readable.
– XAMlMAX
Jul 3 at 8:04


Control


INPC


Control


MvvM


DataTemplate


Style




1 Answer
1



Inheriting from Control works if you add the following ItemContainerStyle to your XAML:


Control


ItemContainerStyle


<ListView Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Header="First" DisplayMemberBinding="{Binding lvi.firstName}"/>
<GridViewColumn Header="Last" DisplayMemberBinding="{Binding lvi.lastName}"/>
</GridView>
</ListView.View>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="{Binding Background}" />
</Style>
</ListView.ItemContainerStyle>
</ListView>





@archer: Does this solve your issue? Please remember to accept the answer then.
– mm8
15 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages