小编Jus*_*tin的帖子

有没有办法指定自定义依赖项属性的默认绑定模式和更新触发器?

我想这样做,默认情况下,当我绑定到我的一个依赖项属性时,绑定模式是双向的,并且update-trigger属性已更改.有没有办法做到这一点?

以下是我的一个依赖项属性的示例:

public static readonly DependencyProperty BindableSelectionLengthProperty =
        DependencyProperty.Register(
        "BindableSelectionLength",
        typeof(int),
        typeof(ModdedTextBox),
        new PropertyMetadata(OnBindableSelectionLengthChanged));
Run Code Online (Sandbox Code Playgroud)

c# data-binding dependency-properties

56
推荐指数
2
解决办法
2万
查看次数

MVVM-如何在文本框中选择文本?

是否有MVVM方式在文本框中选择文本?我使用的MVVM框架是Laurent Bugnion的MVVM Light Toolkit.

c# wpf textbox mvvm mvvm-light

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

创建一个键绑定,每次使用任何数字按下控件时都会触发命令,然后将该数字作为参数传递?

我需要为每个控件+数字组合创建热键,并且不希望创建十个命令.有没有办法做到这一点?

c# wpf xaml command key-bindings

8
推荐指数
2
解决办法
7502
查看次数

如何从wpf中的另一个窗口访问一个窗口的控件(richtextbox)?

我确信这很简单,但我无法弄明白.我在这里和msdn上搜索过,但一直无法找到答案.我需要能够通过richtextbox.Selection.Select(TextPointer1,Textpointer2)设置richtextboxes选择.

wpf richtextbox

7
推荐指数
2
解决办法
1万
查看次数

WPF键盘快捷键 - 为什么这不起作用?

我有以下代码(不起作用):

private void Window_PreviewKeyDown(object sender, KeyEventArgs e) {
    e.Handled = true;
    if ((e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt)) {
        MessageBox.Show("Thanks!");
    }            
}
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用?事件正在解雇,但是

(e.Key == Key.P) && (Keyboard.Modifiers == ModifierKeys.Alt))
Run Code Online (Sandbox Code Playgroud)

永远不会评估为真.我的类似事件使用Ctrl而不Alt是以这种方式工作.我的活动也包括Ctrl Alt工作.

c# keyboard wpf

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

WPF将可视化画笔的视觉效果绑定到不同的窗口

我在设置窗口中需要一个矩形来显示主窗口的缩小版本.这是我现在所使用的非工作代码.有可能做我想做的事吗?

<Rectangle.Fill>
<VisualBrush Stretch="Uniform" Visual="{Binding ElementName=local:MainWindow}" />
</Rectangle.Fill>
Run Code Online (Sandbox Code Playgroud)

wpf visualbrush

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

MVVM-我如何在主视图模型(和其他视图模型)和我的设置对话框之间传播设置?

我正在为我的应用程序构建一个设置对话框,现在所有设置都与主视图模型上的设置相对应,但是当我添加更多视图和视图模型时,有些可能没有.

我需要知道将当前设置加载到设置对话框中的最佳做法是什么,然后如果用户单击"确定",则将设置保存到相应的视图模型.

我不会使用Properties.Settings.Default系统来存储设置,因为我希望我的应用程序尽可能便携,这会将用户范围设置存储在目录中: C:\Users\ username \Local Settings\Application Data\ ApplicationName 而不是在我的应用程序目录中.

如果它有任何区别我使用Laurent Bugnion的MVVM Light Toolkit.

wpf settings mvvm mvvm-light

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

为什么我的所有换行符都从"/ r/n"变为"/ n /",我怎样才能阻止这种情况发生?

我使用XDocument.Save(路径)将文件保存为xml文档,保存并加载文档后,所有换行符都从"/ r/n"更改为"/ n /".为什么会发生这种情况,我该如何解决?

c# xml wpf linq-to-xml

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

Wpf-无法将MenuItem转换为Listbox?

我得到一个非常奇怪的例外.我得到了例外:

"'Set connectionId threw an exception.' Line number '26' and line position '34'."

当我看到内部异常时,我得到:

"Unable to cast object of type 'System.Windows.Controls.MenuItem' to type 'System.Windows.Controls.ListBox'."

我已将此异常的原因缩小到MenuItem包含在此的TreeViewItem样式中TreeView:

<TreeView x:Name="ProjectElementTreeView" ItemsSource="{Binding ProjectElementCollection}"  DisplayMemberPath="Name" Padding="0" SelectedItemChanged="ProjectElementTreeView_SelectedItemChanged" GotKeyboardFocus="ProjectElementTreeView_GotKeyboardFocus">
        <TreeView.Resources>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="HorizontalAlignment" Value="Left" />
                <Setter Property="ContextMenu">
                    <Setter.Value>
                        <ContextMenu>
                            <MenuItem Name="AddProjectElementMenuItem" Header="Add" Click="AddProjectElementMenuItem_Click"/>
                        </ContextMenu>
                    </Setter.Value>
                </Setter>
            </Style>
        </TreeView.Resources>
    </TreeView>
Run Code Online (Sandbox Code Playgroud)

仅当MenuItem具有单击事件处理程序时才会发生异常,即使单击事件处理程序不包含任何代码也会抛出该异常.

wpf xaml exception

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

Wpf- 如何在代码中获取普通 TextBox 的 LineHeight?

我正在开发一个CustomControl继承自TextBox并可以通过Ctrl在拖动鼠标时按住来调整大小的方法,但有时当你调整它的大小时,线条会像这样被切断:

在此处输入图片说明

如果发生这种情况,我想调整选择的高度,这样线条就不会被切断。这是我到目前为止的代码:

double LineHeight = ??;
double requiredHeightAdjustment = this.Height % LineHeight; 

                if (requiredHeightAdjustment != 0)
                {
                    this.Height -= requiredHeightAdjustment;
                }
Run Code Online (Sandbox Code Playgroud)

- 编辑 -

如果将来有人需要这个,这就是我最终得到的:

double fontHeight = this.FontSize * this.FontFamily.LineSpacing;

double requiredHeightAdjustment = this.Height % fontHeight;

var parent = this.Parent as FrameworkElement;

if (requiredHeightAdjustment != 0)
{
    double upwardAdjustedHeight = (fontHeight - requiredHeightAdjustment) + this.Height;

    if (requiredHeightAdjustment >= fontHeight / 2 && this.MaxHeight >= upwardAdjustedHeight
        && (parent == null || parent.ActualHeight >= upwardAdjustedHeight)) …
Run Code Online (Sandbox Code Playgroud)

c# wpf textbox

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