小编Ber*_* L.的帖子

以编程方式从WPF中的代码中删除Strikethrough TextDecoration

我在WPF桌面应用程序中遇到以下问题时遇到问题:

我从后面的代码动态创建TextBlocks并将它们插入到StackPanel中.这项工作到目前为止.当用户将鼠标移到TextBlock上时,Strikthrough将应用于文本块,表示可以通过单击删除该项目.同样,这仍然有效.当鼠标离开文本块时,删除删除线,这里抛出异常,说IsFrozen必须设置为false才能更改TextDecorationCollection对象.我无法弄清楚如何解决这个问题.

这是我的代码:

private void HandleAddedSecondaryDxMouseEnter(Object sender, MouseEventArgs e) {
    TextBlock tbl = (TextBlock)sender;
    tbl.TextDecorations = TextDecorations.Strikethrough;
}

private void HandleAddedSecondaryDxMouseLeave(Object sender, MouseEventArgs e) {
    TextBlock tbl = (TextBlock)sender;
    tbl.TextDecorations.Remove(tbl.TextDecorations[0]);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.

谢谢,伯恩德

.net c# wpf

3
推荐指数
2
解决办法
6263
查看次数

当用户在ItemTemplate文本框中单击时,在ListView中设置SelectedItem

我有以下ListView(简化):

<ListView Name="lvwNotes" KeyUp="lvwNotes_KeyUp">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <DockPanel Background="LightGray">
                     <TextBlock DockPanel.Dock="Right" Text="{Binding Path=Author}" />
                     <TextBlock Text="{Binding Path=Timestamp}" />
                </DockPanel>
                <TextBox Text="{Binding Path=Text}" 
                         GotFocus = "lvwNotes_TextBox_GotFocus"
                         TextWrapping="Wrap" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

通过单击更改所选项目仅在用户使用TextBlocks单击DockPanel时有效,但在单击TextBox时则无效.我想要实现的是将所选项目设置为包含用户单击的TextBox的项目.

我设法通过与TextBox相关的ListViewItem:

private void lvwNotes_TextBox_GotFocus(object sender, RoutedEventArgs e) {
    DependencyObject o = Tools.GetAncestorByType((DependencyObject)sender, typeof(ListViewItem));
    if (!o.Equals(null)) {
        // code to select this ListViewItem
    }
}
Run Code Online (Sandbox Code Playgroud)

但是设定

lvwNotes.SelectedIten = o ;
Run Code Online (Sandbox Code Playgroud)

仍然没有效果.我也尝试过Dispatcher.BeginInvoke的一些技巧,但说实话,我并不确切知道我在那里做什么.

wpf listview textbox selecteditem itemtemplate

1
推荐指数
1
解决办法
3501
查看次数

标签 统计

wpf ×2

.net ×1

c# ×1

itemtemplate ×1

listview ×1

selecteditem ×1

textbox ×1