Shared Events between multiple instances of WPF Custom Controls
I have a couple Custom User Controls written in C# using the composite User Control model (i.e. starting with a UserControl) and when used in isolation, I'm not seeing any problems. But when two instances of the same control are used within the same parent, they seem to be stealing each other's events.
Here is an example. I have a control that uses a ListView. On LayoutUpdated event, I am updating the width of the columns to adjust for content. Not that this adds any value to the question, but here's the code of the handler:
var view = transactionListView.View as GridView;
if (view == null) return;
view.Columns[0].Width = view.Columns[0].ActualWidth;
view.Columns[0].Width = double.NaN;
view.Columns[view.Columns.Count - 1].Width = view.Columns[view.Columns.Count - 1].ActualWidth;
view.Columns[view.Columns.Count - 1].Width = double.NaN;
const int dynamicColumnIndex = 2;
try
{
var dynamicColumnWidth = view.Columns.Where((t, index) => dynamicColumnIndex != index)
.Aggregate(transactionListView.ActualWidth, (current, t) => current - t.ActualWidth);
view.Columns[dynamicColumnIndex].Width = dynamicColumnWidth - 25;
}
catch (Exception ex)
{
// log
}
Again, the sample above is just provided to provide some context, but the other controls that I'm referring to contain completely different implementations.
I thought if I prov开发者_Go百科ided an x:Name for the ListView within the control, it could help, but it didn't. Then I provided an x:Name for the custom control, when I use it. No difference.
The outcome of when I use this control with the ListView is that the second ListView is the only one that maintains the auto-size on the columns. The first one seems un-impacted.
精彩评论