C# 仅附加在第一个用户控件实例上的附加属性
按照Josh Smith关于mvvm工作区(客户视图)的示例,我有一个mainwindow和一个mainwindowviewmodel,其中包含一个“ChattableViewModel”的可观察集合:C# 仅附加在第一个用户控件实例上的附加属性,c#,wpf,mvvm,dependency-properties,attached-properties,C#,Wpf,Mvvm,Dependency Properties,Attached Properties,按照Josh Smith关于mvvm工作区(客户视图)的示例,我有一个mainwindow和一个mainwindowviewmodel,其中包含一个“ChattableViewModel”的可观察集合: 内部类FriendsListViewModel:ObserveObject { #区域约束性质 私人ICollectionView查看好友; 私人可观察收集_chatTab; ... #端区 } 我在xaml中有一个专门用于此集合的区域,如下所示: <ContentControl Gri
内部类FriendsListViewModel:ObserveObject
{
#区域约束性质
私人ICollectionView查看好友;
私人可观察收集_chatTab;
...
#端区
}
我在xaml中有一个专门用于此集合的区域,如下所示:
<ContentControl Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Content="{Binding Path=ChatTabs}" ContentTemplate="{StaticResource ChatTabsTemplate}" />
在我的资源字典中:
<DataTemplate DataType="{x:Type vm:ChatTabViewModel}">
<View:ChatTabView />
</DataTemplate>
<DataTemplate x:Key="ClosableTabItemTemplate">
<DockPanel>
<Button
Command="{Binding Path=CloseCommand}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontSize="9"
FontWeight="Bold"
Margin="0,1,0,0"
Padding="0"
VerticalContentAlignment="Bottom"
Width="16" Height="16"
/>
<ContentPresenter
Content="{Binding Path=Caption, Mode=OneWay}"
VerticalAlignment="Center">
</ContentPresenter>
</DockPanel>
</DataTemplate>
<DataTemplate x:Key="ChatTabsTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"/>
</DataTemplate>
在用户事件中,我在我的集合中添加了一个新的ChattabViewModel,与之相关的视图将显示在主窗口中
但是,当我尝试在ChattabView中的滚动条上添加附加属性时,该属性将仅附加在第一个ChattabViewModel实例上,其他选项卡将不会绑定到附加属性。以下是ChattabView XAML:
<ScrollViewer VerticalScrollBarVisibility="Auto" Grid.Row="0">
<ItemsControl ItemsSource="{Binding Messages}" View:ItemsControlBehavior.ScrollOnNewItem="True">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox IsReadOnly="True" TextWrapping="Wrap" Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
以及所附财产的代码:
namespace GtalkOntre.View
{
/// <summary>
/// Util class to scroll down when a new message is added.
/// </summary>
/// <remarks>attached property called ScrollOnNewItem that when set to true hooks into the INotifyCollectionChanged events of the itemscontrol items source and upon detecting a new item, scrolls the scrollbar to it.</remarks>
public class ItemsControlBehavior
{
static Dictionary<ItemsControl, Capture> Associations = new Dictionary<ItemsControl, Capture>();
public static bool GetScrollOnNewItem(DependencyObject obj)
{
return (bool)obj.GetValue(ScrollOnNewItemProperty);
}
public static void SetScrollOnNewItem(DependencyObject obj, bool value)
{
obj.SetValue(ScrollOnNewItemProperty, value);
}
public static DependencyProperty ScrollOnNewItemProperty =
DependencyProperty .RegisterAttached(
"ScrollOnNewItem",
typeof(bool),
typeof(ItemsControlBehavior),
new UIPropertyMetadata(false, OnScrollOnNewItemChanged));
public static void OnScrollOnNewItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mycontrol = d as ItemsControl;
if (mycontrol == null) return;
bool newValue = (bool)e.NewValue;
if (newValue)
{
mycontrol.Loaded += MyControl_Loaded;
mycontrol.Unloaded += MyControl_Unloaded;
}
else
{
mycontrol.Loaded -= MyControl_Loaded;
mycontrol.Unloaded -= MyControl_Unloaded;
if (Associations.ContainsKey(mycontrol))
Associations[mycontrol].Dispose();
}
}
static void MyControl_Unloaded(object sender, RoutedEventArgs e)
{
var mycontrol = (ItemsControl)sender;
Associations[mycontrol].Dispose();
mycontrol.Unloaded -= MyControl_Unloaded;
}
static void MyControl_Loaded(object sender, RoutedEventArgs e)
{
var mycontrol = (ItemsControl)sender;
var incc = mycontrol.Items as INotifyCollectionChanged;
if (incc == null) return;
mycontrol.Loaded -= MyControl_Loaded;
Associations[mycontrol] = new Capture(mycontrol);
}
class Capture : IDisposable
{
public ItemsControl mycontrol { get; set; }
public INotifyCollectionChanged incc { get; set; }
public Capture(ItemsControl mycontrol)
{
this.mycontrol = mycontrol;
incc = mycontrol.ItemsSource as INotifyCollectionChanged;
incc.CollectionChanged +=incc_CollectionChanged;
}
void incc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
ScrollViewer sv = mycontrol.Parent as ScrollViewer;
sv.ScrollToBottom();
}
}
public void Dispose()
{
incc.CollectionChanged -= incc_CollectionChanged;
}
}
}
}
名称空间gtalcontrol.View
{
///
///添加新消息时向下滚动的Util类。
///
///附加的名为ScrollOnNewItem的属性,当设置为true时,它将钩住itemscontrol items源的INotifyCollectionChanged事件,并在检测到新项目时,将滚动条滚动到该项目。
公共类项控件行为
{
静态字典关联=新字典();
公共静态bool GetScrollOnNewItem(DependencyObject obj)
{
返回(bool)对象获取值(ScrollOnNewItemProperty);
}
公共静态无效设置CrollonneWitem(DependencyObject对象,布尔值)
{
对象设置值(ScrollOnNewItemProperty,值);
}
公共静态从属属性ScrollOnneWitProperty=
从属属性。已注册(
“ScrollOnNewItem”,
类型(bool),
类型(ItemsControlBehavior),
新的UIPropertyMetadata(错误,OnScrolloneWitemChanged);
CrollonnerWitemChanged上的公共静态无效(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
var mycontrol=d作为ItemsControl;
if(mycontrol==null)返回;
bool newValue=(bool)e.newValue;
如果(新值)
{
mycontrol.Loaded+=mycontrol\u Loaded;
mycontrol.unload+=mycontrol\u unload;
}
其他的
{
mycontrol.Loaded-=mycontrol\u Loaded;
mycontrol.unload-=mycontrol\u unload;
if(Associations.ContainsKey(mycontrol))
关联[mycontrol].Dispose();
}
}
静态无效MyControl_已卸载(对象发送器、路由EventTarget e)
{
var mycontrol=(ItemsControl)发送方;
关联[mycontrol].Dispose();
mycontrol.unload-=mycontrol\u unload;
}
已加载静态void MyControl_(对象发送方,路由目标)
{
var mycontrol=(ItemsControl)发送方;
var incc=mycontrol.Items作为INotifyCollectionChanged;
if(incc==null)返回;
mycontrol.Loaded-=mycontrol\u Loaded;
关联[mycontrol]=新捕获(mycontrol);
}
类捕获:IDisposable
{
公共项控件mycontrol{get;set;}
public INotifyCollectionChanged incc{get;set;}
公共捕获(ItemsControl mycontrol)
{
this.mycontrol=mycontrol;
incc=mycontrol.ItemsSource作为INotifyCollectionChanged;
incc.CollectionChanged+=incc_CollectionChanged;
}
void incc_CollectionChanged(对象发送方,notifycollectionchangedventargs e)
{
if(e.Action==NotifyCollectionChangedAction.Add)
{
ScrollViewer sv=mycontrol.Parent作为ScrollViewer;
sv.scrolltobttom();
}
}
公共空间处置()
{
incc.CollectionChanged-=incc_CollectionChanged;
}
}
}
}
那么,为什么附加的属性在chattabviewmodel集合的第一个“chattabview”出现时只绑定一次?因此,只处理第一个ChattableViewModel。
当我将它们全部关闭时,附加属性将在chattabviewmodel的最后一个实例上解除绑定,当我添加新的第一个chattabviewmodel时,该属性将正确绑定。因此,它仅在mainwindowviewmodel的“ChattableViewModel”集合的第一个实例和最后一个实例上触发
经过一周的搜寻,我现在有点绝望
到目前为止,我的假设是:问题可能与我在字典资源中将视图设置为viewmodel的方式有关。视图可能是共享的,第一个滚动条可能只会做出反应。我试图在DataTemplate标记上添加一个
x:Shared=false
属性,但它没有改变任何东西。您没有向我们显示ChattablesTemplate
,因此我只能假设它包含一个TabControl
。如果是这样,这就解释了你看到的行为。TabControl
延迟加载其子选项卡项,因此仅初始化当前视图,并将附加属性应用于该视图。但是,当您切换选项卡时,应该会看到相同的附加属性触发。不是这样吗
至于你的直觉,那不太对。
DataTemplate
正在被共享,但是DataTemplate
用于创建其内容的不同实例,这些实例没有被共享。您没有向我们展示chattablestemplate
,因此我只能假设它包含Ta
namespace GtalkOntre.View
{
/// <summary>
/// Util class to scroll down when a new message is added.
/// </summary>
/// <remarks>attached property called ScrollOnNewItem that when set to true hooks into the INotifyCollectionChanged events of the itemscontrol items source and upon detecting a new item, scrolls the scrollbar to it.</remarks>
public class ItemsControlBehavior
{
static Dictionary<ItemsControl, Capture> Associations = new Dictionary<ItemsControl, Capture>();
public static bool GetScrollOnNewItem(DependencyObject obj)
{
return (bool)obj.GetValue(ScrollOnNewItemProperty);
}
public static void SetScrollOnNewItem(DependencyObject obj, bool value)
{
obj.SetValue(ScrollOnNewItemProperty, value);
}
public static DependencyProperty ScrollOnNewItemProperty =
DependencyProperty .RegisterAttached(
"ScrollOnNewItem",
typeof(bool),
typeof(ItemsControlBehavior),
new UIPropertyMetadata(false, OnScrollOnNewItemChanged));
public static void OnScrollOnNewItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var mycontrol = d as ItemsControl;
if (mycontrol == null) return;
bool newValue = (bool)e.NewValue;
if (newValue)
{
mycontrol.Loaded += MyControl_Loaded;
mycontrol.Unloaded += MyControl_Unloaded;
}
else
{
mycontrol.Loaded -= MyControl_Loaded;
mycontrol.Unloaded -= MyControl_Unloaded;
if (Associations.ContainsKey(mycontrol))
Associations[mycontrol].Dispose();
}
}
static void MyControl_Unloaded(object sender, RoutedEventArgs e)
{
var mycontrol = (ItemsControl)sender;
Associations[mycontrol].Dispose();
mycontrol.Unloaded -= MyControl_Unloaded;
}
static void MyControl_Loaded(object sender, RoutedEventArgs e)
{
var mycontrol = (ItemsControl)sender;
var incc = mycontrol.Items as INotifyCollectionChanged;
if (incc == null) return;
mycontrol.Loaded -= MyControl_Loaded;
Associations[mycontrol] = new Capture(mycontrol);
}
class Capture : IDisposable
{
public ItemsControl mycontrol { get; set; }
public INotifyCollectionChanged incc { get; set; }
public Capture(ItemsControl mycontrol)
{
this.mycontrol = mycontrol;
incc = mycontrol.ItemsSource as INotifyCollectionChanged;
incc.CollectionChanged +=incc_CollectionChanged;
}
void incc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
ScrollViewer sv = mycontrol.Parent as ScrollViewer;
sv.ScrollToBottom();
}
}
public void Dispose()
{
incc.CollectionChanged -= incc_CollectionChanged;
}
}
}
}