我有一个课程如下
public class Account
{
public string TransferFromIndicator { get; set; }
public string AccNickname { get; set; }
public string AccountStatus { get; set; }
public string AccIndex { get; set; }
public SolidColorBrush RowColor { get; set; }
}
正在从后端数据设置除RowColor属性之外的所有数据。在从后端重新获取数据时,我想选择每种替代颜色为黑色,即第一项颜色为白色,下一项颜色为白色。我一直试图用下面的代码片段来做这件事。你能帮我解决一下如何为备用元素设置不同的rowcolor。
List<Account> newList = from x in AccountList select new Account
{
TransferFromIndicator=TransferFromIndicator
AccNickname = AccNickname,
RowColor =
}
答案 0 :(得分:0)
试试这个:
XAML:
<ListBox x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding AccNickname}" Foreground="{Binding RowColor}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
CS:
public class Account
{
public string TransferFromIndicator { get; set; }
public string AccNickname { get; set; }
public string AccountStatus { get; set; }
public string AccIndex { get; set; }
public string RowColor { get; set; }
public Account() { }
public Account(string TransferFromIndicator,string AccNickname,string AccountStatus,string AccIndex,string RowColor)
{
this.TransferFromIndicator = TransferFromIndicator;
this.AccNickname = AccNickname;
this.AccountStatus = AccountStatus;
this.AccIndex = AccIndex;
this.RowColor = RowColor;
}
}
void loadData()
{
List<Account> objAccount = new List<Account>();
objAccount.Add(new Account("data", "data", "data", "data", "White"));
objAccount.Add(new Account("data", "data", "data", "data", "Black"));
objAccount.Add(new Account("data", "data", "data", "data", "White"));
objAccount.Add(new Account("data", "data", "data", "data", "Black"));
objAccount.Add(new Account("data", "data", "data", "data", "White"));
objAccount.Add(new Account("data", "data", "data", "data", "Black"));
lst.ItemsSource = objAccount;
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
loadData();
}
答案 1 :(得分:0)
我通常会使用转换器。互联网上还有一个替代的行颜色实现,用于列表框控件。
按照自己的方式行事:
public class Account
{
public string TransferFromIndicator { get; set; }
public string AccNickname { get; set; }
public string AccountStatus { get; set; }
public string AccIndex { get; set; }
public SolidColorBrush RowColor { get; set; }
}
public class MyViewModel
{
public void BindData()
{
var accounts = new List<Account>
{
new Account { AccIndex = "One", AccNickname = "First Account", AccountStatus = "Closed", TransferFromIndicator = "xyz" },
new Account { AccIndex = "Two", AccNickname = "Second Account", AccountStatus = "Open", TransferFromIndicator = "xyz" },
new Account { AccIndex = "Third", AccNickname = "Third Account", AccountStatus = "Pending", TransferFromIndicator = "xyz" },
new Account { AccIndex = "Fourth", AccNickname = "Fourth Account", AccountStatus = "Closed", TransferFromIndicator = "xyz" },
new Account { AccIndex = "Fifth", AccNickname = "Fifth Account", AccountStatus = "Open", TransferFromIndicator = "xyz" },
new Account { AccIndex = "Sixth", AccNickname = "Sixth Account", AccountStatus = "Pending", TransferFromIndicator = "xyz" }
};
var index = 0;
accounts.ForEach(a => { a.RowColor = ((index % 2 == 0) ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Green)); index++; });
}
}
答案 2 :(得分:0)
首先纠正你的标签。您需要WPF或Windows Phone的解决方案吗?请尝试下面给出的代码。
XAML
<Page.Resources>
<local:AlternateColor x:Name="AlternateColor" />
</Page.Resources>
<Grid>
<ListBox x:Name="lbx">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Background="{Binding Converter={StaticResource AlternateColor}}">
<TextBlock Text="{Binding TransferFromIndicator}" Foreground="GreenYellow" />
<TextBlock Text="{Binding AccNickname}" Foreground="GreenYellow"/>
<TextBlock Text="{Binding AccountStatus}" Foreground="GreenYellow"/>
<TextBlock Text="{Binding AccIndex}" Foreground="GreenYellow"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
获取数据
var list = new List<Account>();
for (int i = 0; i < 10; i++)
{
list.Add(new Account
{
AccIndex = Path.GetRandomFileName(),
AccNickname = Path.GetRandomFileName(),
AccountStatus = Path.GetRandomFileName(),
TransferFromIndicator = Path.GetRandomFileName()
});
}
/*if you don't want to use converter
List<Account> newList = from x in AccountList select s;
bool ThrowBlack = false;
foreach (var item in s)
{
if (ThrowBlack)
{
ThrowBlack = false;
item.RowColor = new SolidColorBrush(Windows.UI.Colors.Black);
}
else
{
ThrowBlack = true;
item.RowColor = new SolidColorBrush(Windows.UI.Colors.White);
}
}
*/
lbx.ItemsSource = list;
转换器
public class AlternateColor : IValueConverter
{
bool ThrowBlack = false;
public object Convert(object value, Type targetType, object parameter, string language)
{
var obj = (Account)value;
if (ThrowBlack)
{
ThrowBlack = false;
obj.RowColor = new SolidColorBrush(Windows.UI.Colors.Black);
return obj.RowColor;
}
else
{
ThrowBlack = true;
obj.RowColor = new SolidColorBrush(Windows.UI.Colors.White);
return obj.RowColor;
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}