在当前项目中,我有一个对象列表。 其中2个属性的类型为string []。
目前,我可以显示带有主要标识符的项目数量。 但除此之外,我想在指定的字符串[]中显示所有不同的字符串。
该课程如下:
public class TimeStamp
{
public int ID { get; set; }
public string[] DaysOfWeek { get; set; }
public string[] HoursOfDay { get; set; }
}
在我的页面中,我有以下内容(按上述方式工作)
<ListView x:Name="listboxFolder1" Grid.Row="2" Margin="5" Grid.ColumnSpan="4"
ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="0 25 0 25" Background="Beige" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal">
<Image Margin="10" Width="50" Height="50" Stretch="Fill" Source="/Images/watch.png">
<Image.BitmapEffect>
<DropShadowBitmapEffect />
</Image.BitmapEffect>
</Image>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock Text="{Binding ID}"/>
<TextBlock Text="{Binding DaysOfWeek}"/>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
完成后,绑定的设置:
static List<iBackupModel.Models.TimeStamp> mySchedules { get; set; }
public Scheduler()
{
InitializeComponent();
listboxFolder1.ItemsSource = Home.mySettings.TimeStamps;
}
但是DaysOfWeek显示如下:String [] - matrix。
我希望它像“星期一|星期五|星期日”。
如果可以将它与固定的一组(在这种情况下是所有工作日)相匹配,并在所有缺失的项目中删除,那将是很好的,但我会感谢第一个。
这可能吗?
对于第一个和第二个选项?
欢迎任何帮助。 提前谢谢。
答案 0 :(得分:3)
问题在于,您将Text
的{{1}}属性绑定到TextBlock
而不是string[]
。
内部发生的事情是WPF调用string
字符串数组上的ToString()
方法,这会导致“System.String []”。
你想要的是绑定到DaysOfWeek
。
有多种方法可以做到这一点。一种强烈推荐的方法是创建一个“视图模型”来保存您实际想要显示的数据,而不是数据,例如,来自数据库或其他类型的模型。< / p>
所以,你可以创建一个string.Join("|", DaysOfWeek })
,如下所示:
TimeStampViewModel
然后,不是绑定到public class TimeStampViewModel
{
public int Id { get; set; }
public string DaysOfWeek { get; set; }
public string HoursOfDay { get; set; }
public TimeStampViewModel(int id, string[] daysOfWeek, string[] hoursOfDay)
{
Id = id;
DaysOfWeek = string.Join("|", daysOfWeek);
HoursOfDay = string.Join("|", hoursOfDay);
}
}
个对象,而是绑定到您在类后面的代码中创建的TimeStamp
个对象,如下所示:
TimeStampViewModel
这假设您的代码隐藏中有一个属性TimeStampViewModels = timeStamps
.Select(x => new TimeStampViewModel(x.Id, x.DaysOfWeek, x.HoursOfDay))
.ToList();
。
如果您想了解有关视图模型的更多信息,我建议您阅读Model-View-ViewModel(MVVM)模式。这里以article published by Microsoft为起点。许多人认为MVVM是WPF(以及其他基于XAML的框架)编程的最佳实践。
要处理关于删除线的“额外信用”问题,这将会更加棘手。您需要将视图模型修改为以下内容:
public List<TimeStampViewModel> TimeStampViewModels { get; set; }
与此同时,您的XAML需要在public class TimeStampViewModel
{
public int Id { get; set; }
public List<OptionalDayOfWeek> DaysOfWeek { get; set; }
public string HoursOfDay { get; set; }
public TimeStampViewModel(int id, string[] daysOfWeek, string[] hoursOfDay)
{
Id = id;
DaysOfWeek =
new[] { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
.GroupJoin(daysOfWeek, day => day, dayOfWeek => dayOfWeek, (day, matches) => new OptionalDayOfWeek { DayOfWeek = day, IsAvailable = matches.Any() })
.ToList();
HoursOfDay = string.Join("|", hoursOfDay);
}
}
public class OptionalDayOfWeek
{
public string DayOfWeek { get; set; }
public bool IsAvailable { get; set; }
}
之类的内容中显示星期几,其中每个项目都绑定到ItemsControl
。然后,您可以使用OptionalDayOfWeek
来确定IValueConverter
是否应包含TextDecorations
。 (对不起,我现在没有时间写这个部分,但希望这足以让你开始。)
答案 1 :(得分:0)
您可以按如下方式修改类:
public class TimeStamp
{
private string _HourList;
private string _DayList;
private string[] _DaysOfWeek;
private string[] _HoursOfDay;
public string[] HoursOfDay
{
get { return _HoursOfDay; }
set
{
_HoursOfDay = value;
_HourList = String.Join("|", value);
}
}
public int ID { get; set; }
public string[] DaysOfWeek
{
get { return _DaysOfWeek; }
set
{
_DaysOfWeek = value;
_DayList = String.Join("|", value);
}
}
public string DayLis
{
get { return _DayList; }
}
public string HourList
{
get { return _HourList; }
}
}
定义类的优点如下:
每当你为字符串数组
DaysOfWeek
赋值时,它都会 在所有数组元素中自动创建一个字符串DayList
ware与分隔符|
组合在一起,因为DayList
是一个读取 只有属性,你不能赋值给它。你会得到价值 来自此属性以及绑定时
因此您的xamal
标记将如下所示:
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock Text="{Binding ID}"/>
<TextBlock Text="{Binding DayLis}"/>
</StackPanel>
答案 2 :(得分:0)
您希望将DaysOfWeek显示为TextBlock中的文本。 DaysOfWeek是一个字符串数组,不是字符串,因此它不会按预期显示。您必须转换为字符串,并且可以在转换函数中指定格式。您必须将字符串数组转换为WPF格式化字符串
第一个解决方案:更好但更复杂的方式
public class WeekDaysFormatConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value != null && value is string[])
{
return string.Join("|", value as string[]);
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
并应用它:
<Window.Resources>
<local:WeekDaysFormatConverter x:Key="WeekDaysFormatConverter" />
</Window.Resources>
...
<TextBlock Text="{Binding DaysOfWeek Converter={StaticResource WeekDaysFormatConverter}}"/>
第二个解决方案:只需使用属性指定格式:
public class TimeStamp
{
public int ID { get; set; }
public string[] DaysOfWeek { get; set; }
public string[] HoursOfDay { get; set; }
public string DaysOfWeekFormatted
{
get
{
return DaysOfWeek != null ? string.Join("|", DaysOfWeek) : string.Empty;
}
}
}
并应用它:
<TextBlock Text="{Binding DaysOfWeekFormatted}"/>
答案 3 :(得分:0)
我基本同意devuxer的做法。另一方面,如果你想保留你的模型类,最简单的方法来解决你的第二个问题是将字符串数组转换为TextDecorations。
[ValueConversion(typeof(string[]), typeof(TextDecorations))]
public class DecorationsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var source = value as string[];
if (source == null)
return DependencyProperty.UnsetValue;
return source.Contains(parameter as string) ? null : TextDecorations.Strikethrough;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
更改ListView以使用该转换器。
<ListView x:Name="listboxFolder1" Grid.Row="2" Margin="5" Grid.ColumnSpan="4">
<ListView.Resources>
<local:DecorationsConverter x:Key="DecorationsConverterKey"/>
</ListView.Resources>
<ListView.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="0 25 0 25" Background="Beige" HorizontalAlignment="Stretch">
<StackPanel Orientation="Horizontal">
<Image Margin="10" Width="50" Height="50" Stretch="Fill" Source="/Images/watch.png">
<Image.BitmapEffect>
<DropShadowBitmapEffect />
</Image.BitmapEffect>
</Image>
<StackPanel Orientation="Vertical" VerticalAlignment="Center">
<TextBlock Text="{Binding ID}"/>
<TextBlock>
<Run Text="Sunday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Sunday}"/>
<Run Text="|"/>
<Run Text="Monday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Monday}"/>
<Run Text="|"/>
<Run Text="Tuesday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Tuesday}"/>
<Run Text="|"/>
<Run Text="Wednesday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Wednesday}"/>
<Run Text="|"/>
<Run Text="Thursday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Thursday}"/>
<Run Text="|"/>
<Run Text="Friday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Friday}"/>
<Run Text="|"/>
<Run Text="Saturday"
TextDecorations="{Binding DaysOfWeek, Converter={StaticResource DecorationsConverterKey}, ConverterParameter=Saturday}"/>
</TextBlock>
</StackPanel>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>