【问题标题】:ControlTemplate on Style WPF / MVVM样式 WPF / MVVM 上的 ControlTemplate
【发布时间】:2018-02-01 13:44:11
【问题描述】:

我试图弄清楚如何为我在 UserControl 中的所有文本框定义一个 ControlTemplate,我尝试了几件事,但似乎没有用。

我需要将 KeyBinding 绑定到命令:

<TextBox.InputBindings>
      <KeyBinding Key="Enter" Command="{Binding SaveConfigCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}"/>
</TextBox.InputBindings>

到目前为止,这是可行的,但是将相同的 sn-p 添加到所有文本框并不是一个好习惯。所以我试图把它放在样式上,但我得到 NullReferenceException

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="TextBox">
            <Grid>
                <TextBox Text="{TemplateBinding}"/>
                <ContentPresenter>
                    <ContentPresenter.InputBindings>
                        <KeyBinding Key="Enter" Command="{Binding SaveConfigCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}"/>
                    </ContentPresenter.InputBindings>
                </ContentPresenter>
            </Grid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

有什么想法吗?提前致谢!

编辑以供将来参考: 虽然答案是正确的,但我想指出 TemplateBinding Text 在控件中已经绑定 Text 属性时是不够的,因为 TemplateBinding 只是一个 OneWay 绑定,你必须使用以下:

Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"

【问题讨论】:

    标签: c# wpf xaml mvvm


    【解决方案1】:

    TemplateBinding 用于绑定到模板定义中的控件属性。进一步将您的 InputBindings 放入 TextBox。

    <Style x:Key="tbxStyle" TargetType="TextBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TextBox">
                            <Grid>
                                <TextBox Text="{TemplateBinding Text}">
                                    <TextBox.InputBindings>
                                        <KeyBinding Key="Enter" Command="{Binding SaveConfigCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}"/>
                                    </TextBox.InputBindings>
                                </TextBox>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    
    <TextBox Text="{Binding SelectedItem}" Style="{StaticResource tbxStyle}">
    

    【讨论】:

    • 不起作用,得到另一个(不同的)异常:System.InvalidOperationException:'在处理名为''类型为'System.Windows.Controls.TextBox'的元素的模板时发现引用中的无限循环'.'
    • 你说得对,我忘了包括Style="{StaticResource tbxStyle}。您有什么理由必须指定名称才能使其工作?我希望这适用于所有文本框,这就是我使用 TargetType 的原因。
    • 问题是,您在 TextBox 模板中使用了 TextBox-Element。
    • 我明白了,我也意识到我必须将 {TemplateBinding} 包含到所有不是 TextBox 的默认属性中。
    • 我已经用更多信息编辑了我的问题,因为解决方案没有完全发挥作用,因为 TemplateBinding 是 OneWay 绑定,您需要这样做:Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"
    猜你喜欢
    • 2015-02-28
    • 2022-11-07
    • 2010-10-31
    • 2010-12-11
    • 1970-01-01
    • 2016-02-12
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多