首页 \ 问答 \ WPF:如何在序列化的事件处理程序中获取调用对象的名称或其他唯一标识符?(WPF: How to get name or other unique identifier of calling object in event handler for serialization?)

WPF:如何在序列化的事件处理程序中获取调用对象的名称或其他唯一标识符?(WPF: How to get name or other unique identifier of calling object in event handler for serialization?)

我有一个GridView,并希望序列化会话中的列宽 。 我对如何实现这一点的想法是将行为附加到GridViewColumns,使得每次更改列的宽度时调用附加的事件处理程序并存储新的宽度。 这已经很好。

唯一剩下的问题是:

我如何知道事件处理程序哪个GridViewColumn发送了事件 ? 我显然需要知道,为了能够存储宽度,并稍后在恢复时在正确的列上设置宽度。 理想情况下,我想使用XAML中指定的名称作为列标识符。

这是我的代码。 XAML

<GridView>
  <GridViewColumn x:Name="GridColumn0"
    HeaderTemplate="{StaticResource GridViewHeaderTemplate}" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}" 
    Header="{x:Static strings:Strings.MainWindow_AppLog_Header_Severity}"
    behaviors:GridViewBehaviors.PersistColumnWidth="True">

C# (请向下滚动 - 底部的问题):

// Register the property used in XAML
public static readonly DependencyProperty PersistColumnWidthProperty =
     DependencyProperty.RegisterAttached("PersistColumnWidth", typeof(bool), typeof(GridViewBehaviors),
     new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPersistColumnWidthChanged)));

// Provide read access to the value
public static bool GetPersistColumnWidth(DependencyObject d)
{
    return (bool)d.GetValue(PersistColumnWidthProperty);
}

// Provide write access to the value (set from XAML)
public static void SetPersistColumnWidth(DependencyObject d, bool value)
{
    d.SetValue(PersistColumnWidthProperty, value);
}

// This gets called once when the XAML is compiled to BAML
// Set the event handler
private static void OnPersistColumnWidthChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // Couple the UI event with a delegate
    if ((bool)args.NewValue)
        ((INotifyPropertyChanged)column).PropertyChanged += new PropertyChangedEventHandler(PersistWidth);
    else
        ((INotifyPropertyChanged)column).PropertyChanged -= new PropertyChangedEventHandler(PersistWidth);
}

// Deal with the events
static void PersistWidth(object sender, PropertyChangedEventArgs e)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // We are only interested in changes of the "ActualWidth" property
    if (e.PropertyName != "ActualWidth")
        return;

    // Ignore NaNs
    if (column.ActualWidth == double.NaN)
        return;

    // Persist the width here
    // PROBLEM:
    // How to get a unique identifier for column, ideally its name set in XAML?
}

I have a GridView and want to serialize column widths across sessions. My idea of how to accomplish this is to attach a behavior to the GridViewColumns in such a way that each time the width of a column is changed the attached event handler is called and stores the new width. This already works well.

The only remaining problem:

How do I know in the event handler which GridViewColumn sent the event? I obviously need to know that in order to be able to store the width and later set the width on the correct column when restoring. Ideally I would like to use the name specified in XAML as column identifier.

Here is my code. XAML:

<GridView>
  <GridViewColumn x:Name="GridColumn0"
    HeaderTemplate="{StaticResource GridViewHeaderTemplate}" HeaderContainerStyle="{StaticResource GridViewHeaderStyle}" 
    Header="{x:Static strings:Strings.MainWindow_AppLog_Header_Severity}"
    behaviors:GridViewBehaviors.PersistColumnWidth="True">

C# (please scroll down - question at bottom):

// Register the property used in XAML
public static readonly DependencyProperty PersistColumnWidthProperty =
     DependencyProperty.RegisterAttached("PersistColumnWidth", typeof(bool), typeof(GridViewBehaviors),
     new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnPersistColumnWidthChanged)));

// Provide read access to the value
public static bool GetPersistColumnWidth(DependencyObject d)
{
    return (bool)d.GetValue(PersistColumnWidthProperty);
}

// Provide write access to the value (set from XAML)
public static void SetPersistColumnWidth(DependencyObject d, bool value)
{
    d.SetValue(PersistColumnWidthProperty, value);
}

// This gets called once when the XAML is compiled to BAML
// Set the event handler
private static void OnPersistColumnWidthChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // Couple the UI event with a delegate
    if ((bool)args.NewValue)
        ((INotifyPropertyChanged)column).PropertyChanged += new PropertyChangedEventHandler(PersistWidth);
    else
        ((INotifyPropertyChanged)column).PropertyChanged -= new PropertyChangedEventHandler(PersistWidth);
}

// Deal with the events
static void PersistWidth(object sender, PropertyChangedEventArgs e)
{
    GridViewColumn column = sender as GridViewColumn;
    if (column == null)
        return;

    // We are only interested in changes of the "ActualWidth" property
    if (e.PropertyName != "ActualWidth")
        return;

    // Ignore NaNs
    if (column.ActualWidth == double.NaN)
        return;

    // Persist the width here
    // PROBLEM:
    // How to get a unique identifier for column, ideally its name set in XAML?
}

原文:https://stackoverflow.com/questions/4619467
更新时间:2022-01-04 22:01

最满意答案

使用translateY技巧将您的标签置于绝对位置并以此方式居中:

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
    position: absolute !important;
    left: 0;
    z-index: 1;
    height: 100%;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}

然后添加相对于按钮的位置:

.round-button{
    position:relative;
}

工作示例http://jsfiddle.net/4qqczsw0/3/


Position your label absolute and centre it that way using the translateY trick:

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
    position: absolute !important;
    left: 0;
    z-index: 1;
    height: 100%;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
}

Then add position relative to your button:

.round-button{
    position:relative;
}

Working example http://jsfiddle.net/4qqczsw0/3/

相关问答

更多
  • 在我的开发环境中编译资产完全指向错误: rake资产:预编译 我有一个名为Editor的DataTables插件的过时版本。 我从模板中生成了这个项目。 由于我没有使用编辑器,我只是将其删除,这个错误就消失了。 Compiling the assets in my development environment pointed exactly to the error: rake assets:precompile I had an outdated version of a DataTables plug ...
  • 发行说明告诉你:( http://blog.getbootstrap.com/2013/08/19/bootstrap-3-released/ ) 默认情况下更好的框模型。 Bootstrap中的所有内容都可以获取框尺寸:边框,使更容易的尺寸选项和增强的网格系统。 就个人而言,我认为大部分的收益都是电网系统。 在Twitter的Bootstrap中,所有的网格都是流畅的。 列定义为总宽度的百分比。 但是沟槽具有固定的像素宽度。 默认情况下,该列两边的填充为15px。 宽度(以像素为单位)和百分比的组合可能很 ...
  • 使用translateY技巧将您的标签置于绝对位置并以此方式居中: label { display: inline-block; max-width: 100%; margin-bottom: 5px; font-weight: 700; position: absolute !important; left: 0; z-index: 1; height: 100%; right: 0; top: 50%; transf ...
  • 你也可以在sass文件中使用它 - 尝试下面的代码 * &:before, &:after +box-sizing(border-box) input +box-sizing(content-box) 现在,更少的代码转换为sass代码。 如果Less代码正常工作,那么这个sass代码肯定会对你有用 you can use it within sass file also- try below code * &:before, &:after +bo ...
  • 通过查看您的图片:这不是大小调整问题,但输入字段空间正在显示,因为默认情况下不同的浏览器具有不同的样式。 所以你应该尝试使用margin: 0; padding: 0; margin: 0; padding: 0; 用于输入字段 定义高度也可以帮到你。 并为这些元素应用显示内联或内联块或表格单元格,并将其垂直对齐到顶部或中间。 It was boxing sizing issue, for some fields I had to set it to content-box.
  • 这个伎俩。 我正在针对错误的元素。 .ui-radiobutton-box { -webkit-box-sizing: content-box; -moz-box-sizing: content-box; box-sizing: content-box; } This did the trick. I was targetting the wrong element. .ui-radiobutton-box ...
  • 看来,更改导航.navbar-default背景的关键是background-image与.navbar-default上的渐变相关联。 如果您对该属性不应用,则应获得所需的效果。 .navbar-default{ background-color: red; background-image: none; } 正如您在文本颜色更改中看到的那样,其他覆盖应该正常工作。 DEMO It appears the key for changing the background in the navbar ...
  • box-sizing用于操纵元素的碰撞盒,而不是视觉形状。 您正在寻找的可能是border-radius ,我认为bootstrap适用于您的输入。 你应该可以通过在CSS代码中添加border-radius: 0来修复它。 由于Stack上没有引导程序,因此输入不会呈现圆角。 box-sizing is used to manipulate the collision-box of elements, not the visual shape. What you are looking for might ...
  • 如果没有看到实际代码,就无法为您的情况提供实际的标记。 然而,问题的根源是特异性 。 你可以在这里阅读更多相关信息。 基本上,您在CSS中使用的选择器越多,它在特定性方面的排名就越高。 例如,引用上面的HTML, .text-title {} 不是很具体。 .label-text .text-title {} 更具体,优先。 .label .label-text .text-title {} 甚至更具体。 和: div.label > .label-text > .text-title {} 更具体 ...
  • dc.leaflet.js将接管它在构造函数中给出的整个
    ,所以只需将标题移出div,对吧? 因为这意味着地图不占用整个引导列,所以我们需要将它放在自己的子div中。
    Media
    ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)