我在控件上有一个ObservableCollection类型的附加属性.如果我在集合中添加或删除项目,则ui不会更新.但是,如果我用新的替换集合,则ui会更新ViewModel.
有人能给我一个我在Dependency对象中需要做什么的例子,以便它可以处理集合中的变化吗?
下面列出了依赖项对象的一部分:
public class RadCalendarBehavior : DependencyObject
{
private static void OnSpecialDaysChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var calendar = d as RadCalendar;
if (e.NewValue != null)
{
calendar.DayTemplateSelector = new SpecialDaySelector((ObservableCollection<DateTime>)e.NewValue, GetSpecialDayTemplate(d));
}
}
public static ObservableCollection<DateTime> GetSpecialDays(DependencyObject obj)
{
return (ObservableCollection<DateTime>)obj.GetValue(SpecialDaysProperty);
}
public static void SetSpecialDays(DependencyObject obj, ObservableCollection<DateTime> value)
{
obj.SetValue(SpecialDaysProperty, value);
}
public static readonly DependencyProperty SpecialDaysProperty =
DependencyProperty.RegisterAttached("SpecialDays", typeof(ObservableCollection<DateTime>), typeof(RadCalendarBehavior), new UIPropertyMetadata(null, OnSpecialDaysChanged));
}
}
Run Code Online (Sandbox Code Playgroud)
我知道我需要注册该集合已经更改,但我不确定如何在依赖项属性中执行此操作
这真让我抓狂.我有一个DataGrid,它有一个DataGridComboBoxColumn,我希望用户可以使用它来进行选择.这是我网格的基本轮廓.
<DataGrid ItemsSource="{Binding GoalList}" DockPanel.Dock="Bottom" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn ItemsSource="{Binding LifeAreaList}" Header="Life Area"/>
<DataGrid.Columns>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
DataGrid绑定到Goal类型的对象集合.每个目标都有一个LifeArea类型的属性.每个LifeArea都具有LifeAreaId和Name属性.
数据上下文包含一个可观察的目标集合:目标列表和生命区域列表:LifeAreaList.我希望用户能够为目标选择不同的生活区域.生命区域的名称也需要是显示的值.
编辑
解决方案是必须将DataGridComboBoxColumn的ItemsSource设置为静态资源.另一种选择是通过代码设置ItemsSource.
最后我有:
<DataGridComboBoxColumn x:Name="_lifeAreaComboBoxColumn" SelectedItemBinding="{Binding LifeArea}" DisplayMemberPath="Name" Header="Life Area">
Run Code Online (Sandbox Code Playgroud)
在后面的代码我设置ItemsSource:
_lifeAreaComboBoxColumn.ItemsSource = LifeAreaDAL.GetLifeAreas();
Run Code Online (Sandbox Code Playgroud)
当我有机会将其转换为StaticResource时.
我想在内容无效时将样式应用于文本框.
以下样式有效:
<Style x:Key="textBoxNormalStyle" TargetType="{x:Type TextBox}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="ForceCursor" Value="False"/>
<Setter Property="Foreground" Value="{StaticResource TextColor}"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="3,0,3,0"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border Background="{StaticResource TextBackColor}" BorderBrush="{StaticResource BorderColor}" x:Name="Bd" CornerRadius="4" BorderThickness="2">
<ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="16"/>
<Setter Property="ForceCursor" Value="False"/>
<Setter Property="Foreground" Value="{StaticResource TextColor}"/>
<Setter …
Run Code Online (Sandbox Code Playgroud) 我无法将图像资源转换为byte [].
例如,我有以下资源:
pack://application:,,,/AppName;component/Assets/Images/sampleimage.jpg
Run Code Online (Sandbox Code Playgroud)
在我的程序中.如何将其转换为byte [].
我尝试过使用BitMapImage,但是ImageSource在初始化后最终为null.
我想创建一个可见性转换器,如果observablecollection为空或null,则显示内容.由于此转换器将在许多屏幕上使用,因此每个集合将保持不同的类型(T).
如何获取未知类型的ObservableCollection的引用.这是我到目前为止:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return Visibility.Visible;
if (value is ObservableCollection<object>)
{
var col = value as ObservableCollection<object>;
return col.Count > 0 ? Visibility.Hidden : Visibility.Visible;
}
return Binding.DoNothing;
}
Run Code Online (Sandbox Code Playgroud)