C# Silverlight,网格内的文本块
我是silverlight的新手,有人让我修改它。我的问题很简单(如果在asp.net webforms中完成)。 基本上,在网格中,我想把年份附加到类似这样的东西上C# Silverlight,网格内的文本块,c#,asp.net,silverlight,C#,Asp.net,Silverlight,我是silverlight的新手,有人让我修改它。我的问题很简单(如果在asp.net webforms中完成)。 基本上,在网格中,我想把年份附加到类似这样的东西上 Jan + "-" + DateTime.Now.Year.ToString() Feb + "-" + DateTime.Now.Year.ToString() 等等等等 xaml看起来像这样 <Grid x:Name="ContentGrid" HorizontalAlignment="Stretch" Vertic
Jan + "-" + DateTime.Now.Year.ToString()
Feb + "-" + DateTime.Now.Year.ToString()
等等等等
xaml看起来像这样
<Grid x:Name="ContentGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0">
<Grid.Resources>
<DataTemplate x:Key="mykey1">
<Grid >....</Grid>
</DataTemplate>
<DataTemplate x:Key="mykey2">
<Grid >....</Grid>
</DataTemplate>
<DataTemplate x:Key="mykey3">
<Grid >
<StackPanel Orientation="Vertical">
<Border BorderBrush="{StaticResource LogicaPebbleBlackBrush}" BorderThickness="1">
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Style="{StaticResource HeaderStackPanel}">
<TextBlock Style="{StaticResource HeaderTextBlock}" Text="Jan-2013" Width="75" TextAlignment="Center"/>
<TextBlock Style="{StaticResource HeaderTextBlock}" Text="Feb-2013" Width="75" TextAlignment="Center"/>
</StackPanel>
</StackPanel>
</Grid>
</DataTemplate>
</ Grid>
</DataTemplate>
....
....
我只是想让这一年充满活力,这样它每年都会改变。请帮助。我不知道是否可以直接在XAML中完成 最好使用绑定。在Silverlight中,可以将大多数数据源绑定到代码隐藏中的属性(即ViewModel) 简言之:
<TextBlock Text="{Binding Path=Year, Mode=OneWay}" />
public class ViewModel : INotifyPropertyChanged
{
private DateTime _year = DateTime.Now;
public DateTime Year
{
get { return _year; } // <--- append whatever here or in the setter
set
{
_year = value;
if( this.PropertyChanged != null )
{
this.PropertyChanged( this, new PropertyChangedEventArgs( "Year" ) );
}
}
}
...
}
您的ViewModel属性将如下所示:
<TextBlock Text="{Binding Path=Year, Mode=OneWay}" />
public class ViewModel : INotifyPropertyChanged
{
private DateTime _year = DateTime.Now;
public DateTime Year
{
get { return _year; } // <--- append whatever here or in the setter
set
{
_year = value;
if( this.PropertyChanged != null )
{
this.PropertyChanged( this, new PropertyChangedEventArgs( "Year" ) );
}
}
}
...
}
公共类视图模型:INotifyPropertyChanged
{
private DateTime _year=DateTime.Now;
公共日期时间年
{
获取{return\u year;}/这可能会对您有所帮助。它只是xaml:
<Grid.Resources>
<System:DateTime x:Key="DateTimeDataSource"/>
</Grid.Resources>
<TextBlock DataContext="{Binding Source={StaticResource DateTimeDataSource}}"
Text="{Binding Today.Year}">
</TextBlock>
您还可以显示其他日期时间属性:Now.Day、Today.Month等。我重新阅读了您的问题,我认为您需要这样做。
由于您的工作在网格中,因此可以命名您的文本块
<TextBlock Style="{StaticResource HeaderTextBlock}" x:Name="JanTB" Width="75" TextAlignment="Center"/>
我希望这能解决你的问题。你能给你的文本块一个ID并在代码隐藏中设置它们的.Text
属性吗?我试过了,但无法在代码隐藏中访问它们。我有错误。System.Windows.Markup.XamlParseException:未知元素:DateTime。[行:29位置:66]第29行表示您已将其添加到命名空间中,并且在项目中是否引用了它?xmlns:System=“clr namespace:System;assembly=mscorlib”