C
C#13mo ago
TheOneAndOnly

❔ How to bind data to a listbox that doesnt come out of a database?

Title pretty much says it all. I have the following code but it doesnt work:
<ListBox x:Name="list_ideologies" Margin="0,0,1031,10" Grid.RowSpan="2" ItemsSource="{Binding data.ideologies}"/>
<ListBox x:Name="list_ideologies" Margin="0,0,1031,10" Grid.RowSpan="2" ItemsSource="{Binding data.ideologies}"/>
17 Replies
SinFluxx
SinFluxx13mo ago
What does your code for your data look like?
TheOneAndOnly
TheOneAndOnlyOP13mo ago
public ProjectData()
{
countries = new Countries();
focusIcons = new List<FocusIcon>();
ideologies = new List<Ideology>();
projectFolder = new Dictionary<string, string>();
}
public ProjectData()
{
countries = new Countries();
focusIcons = new List<FocusIcon>();
ideologies = new List<Ideology>();
projectFolder = new Dictionary<string, string>();
}
ideologies is a simple list storing all the ideologies
SinFluxx
SinFluxx13mo ago
how/where are you adding data to ideologies? I've assumed by "doesn't work" you mean that you're not getting any errors/binding errors, just no data showing in the listbox?
TheOneAndOnly
TheOneAndOnlyOP13mo ago
Yeah, the list is correctly filled I have checked that I run this
projectData.ideologies = ProjectLoader.LoadInIdeologies(projectData.GetProjectFolder("ideologies"));
projectData.ideologies = ProjectLoader.LoadInIdeologies(projectData.GetProjectFolder("ideologies"));
When starting up the menu window and loading in all the data/settings
SinFluxx
SinFluxx13mo ago
Don't know what your Ideology class looks like but no reason why it shouldn't work if your ideologies list is populated
No description
TheOneAndOnly
TheOneAndOnlyOP13mo ago
C#
public class Ideology
{
public string name { get; set; }
public string color { get; set; }
private List<SubIdeology> subideologiesl;
private List<string> dynamic_faction_names;
private List<NameValue> rules;
private List<NameValue> modifiers;
public bool can_collaborate { get; set; }
public bool ai_fascist { get; set; }
public double war_impact_on_world_tension { get; set; }
public double faction_impact_on_world_tension { get; set; }
public double ai_ideology_wanted_units_factor { get; set; }

public Ideology(string _name)
{
dynamic_faction_names = new List<string>();
rules = new List<NameValue>();
modifiers = new List<NameValue>();
subideologiesl = new List<SubIdeology>();
name = _name;
}

public void AddFactionName(string name)
{
dynamic_faction_names.Add(name);
}
public void AddRule(string name, string value)
{
rules.Add(new NameValue(name, value));
}
public void AddModifier(string name, string value)
{
modifiers.Add(new NameValue(name, value));
}
public void AddSubIdeology(SubIdeology subIdeology)
{
subideologiesl.Add(subIdeology);
}
}
public struct NameValue
{
public string name;
public string value;
public NameValue(string name, string value) { this.name = name; this.value = value; }
}

public struct SubIdeology
{
public string name;
public bool can_be_randomly_selected;
public List<NameValue> modifiers;
public SubIdeology(string name) { this.name = name; }
}
C#
public class Ideology
{
public string name { get; set; }
public string color { get; set; }
private List<SubIdeology> subideologiesl;
private List<string> dynamic_faction_names;
private List<NameValue> rules;
private List<NameValue> modifiers;
public bool can_collaborate { get; set; }
public bool ai_fascist { get; set; }
public double war_impact_on_world_tension { get; set; }
public double faction_impact_on_world_tension { get; set; }
public double ai_ideology_wanted_units_factor { get; set; }

public Ideology(string _name)
{
dynamic_faction_names = new List<string>();
rules = new List<NameValue>();
modifiers = new List<NameValue>();
subideologiesl = new List<SubIdeology>();
name = _name;
}

public void AddFactionName(string name)
{
dynamic_faction_names.Add(name);
}
public void AddRule(string name, string value)
{
rules.Add(new NameValue(name, value));
}
public void AddModifier(string name, string value)
{
modifiers.Add(new NameValue(name, value));
}
public void AddSubIdeology(SubIdeology subIdeology)
{
subideologiesl.Add(subIdeology);
}
}
public struct NameValue
{
public string name;
public string value;
public NameValue(string name, string value) { this.name = name; this.value = value; }
}

public struct SubIdeology
{
public string name;
public bool can_be_randomly_selected;
public List<NameValue> modifiers;
public SubIdeology(string name) { this.name = name; }
}
That is my ideology class
SinFluxx
SinFluxx13mo ago
copied your class and still works for me
No description
TheOneAndOnly
TheOneAndOnlyOP13mo ago
That is really weird than
SinFluxx
SinFluxx13mo ago
are other bindings on the window working ok? try setting DisplayMemberPath?
TheOneAndOnly
TheOneAndOnlyOP13mo ago
Its only this

<Window x:Class="LOTRModdingProgram.Windows.IdeologiesWindow"
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:LOTRModdingProgram.Windows"
mc:Ignorable="d"
Title="IdeologiesWindow" Height="850" Width="1200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="79*"/>
<RowDefinition Height="756*"/>
</Grid.RowDefinitions>
<ListBox x:Name="list_ideologies" Margin="0,0,1031,10" Grid.RowSpan="2" DisplayMemberPath="name" ItemsSource="{Binding data.ideologies}"/>
</Grid>
</Window>

<Window x:Class="LOTRModdingProgram.Windows.IdeologiesWindow"
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:LOTRModdingProgram.Windows"
mc:Ignorable="d"
Title="IdeologiesWindow" Height="850" Width="1200">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="79*"/>
<RowDefinition Height="756*"/>
</Grid.RowDefinitions>
<ListBox x:Name="list_ideologies" Margin="0,0,1031,10" Grid.RowSpan="2" DisplayMemberPath="name" ItemsSource="{Binding data.ideologies}"/>
</Grid>
</Window>
But it still doesnt work
SinFluxx
SinFluxx13mo ago
can you update your margin to 0 and try?
Pobiega
Pobiega13mo ago
Since ideologies is a list, if the data binding is set up before that list is filled, it will show nothing if you instead make it an ObservableCollection<Ideology>, you might get better results
TheOneAndOnly
TheOneAndOnlyOP13mo ago
C#
public IdeologiesWindow(MenuWindow menu)
{
this.menu = menu;
data = menu.projectData;
settings = menu.settingData;
InitializeComponent();
}
C#
public IdeologiesWindow(MenuWindow menu)
{
this.menu = menu;
data = menu.projectData;
settings = menu.settingData;
InitializeComponent();
}
InitializeComponent is run as last shouldnt that assure that the list is defined before binding?
Pobiega
Pobiega13mo ago
¯\_(ツ)_/¯
TheOneAndOnly
TheOneAndOnlyOP13mo ago
Fair enough Will try to make it a observable and hope that works than
Pobiega
Pobiega13mo ago
using a list as a datasource should work fine, but as said, no reactivity.
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server