Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background / Foreground Capability in Custom ListView Items

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

<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:

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.

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?

like image 305
archer Avatar asked Jan 17 '26 09:01

archer


1 Answers

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

<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>
like image 138
mm8 Avatar answered Jan 20 '26 00:01

mm8



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!