gpt4 book ai didi

c# - 通过 wpf 中的内容呈现器将公共(public)字符串属性绑定(bind)到 TextBox 文本

转载 作者:行者123 更新时间:2023-12-03 10:26:21 24 4
gpt4 key购买 nike

我创建了一个自定义 wpf 控件,希望可以反复重用它以减少编写或复制和粘贴一堆 xaml 代码所花费的时间。看起来,很简单。它用于 Label 和 TextBox 对,所以:

Label here
[ this is the text box (or other control) ]

它是这样称呼的:
<controls:LabeledContentControl Margin="5"
MaxHeight="25"
LabelText="{Binding LabelString}"
Content="{Binding LabelTextBoxTestString, ValidatesOnNotifyDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>

或者
<controls:LabeledContentControl Margin="5"
LabelText="{Binding LabelString}">
<TextBox Text="{Binding LabelTextBoxTestString,
ValidatesOnNotifyDataErrors=True,
UpdateSourceTrigger=PropertyChanged,
Delay=100}" />
</controls:LabeledContentControl>
LabeledContentControl扩展 ContentControl ,c# 代码似乎不相关,因为它是依赖属性及其 setter 。以及与 Microsoft 引用文档中实现的两个构造函数类似的构造函数,用于扩展 ContentControl 的其他控件。

https://referencesource.microsoft.com/#PresentationFramework/Framework/System/Windows/Controls/HeaderedContentControl.cs

在使用第一种方法进行标记内容控件时,我遇到了绑定(bind)支持 View 模型属性/字段集的问题。

控件的 XAML 代码如下所示:
<Style x:Key="{x:Type controls:LabeledContentControl}" TargetType="{x:Type controls:LabeledContentControl}">
<!-- other setters.... -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:LabeledContentControl}">
<Border Name="OuterBd" BorderThickness="{TemplateBinding BorderThickness}" >
<Border Name="InnerBd" Background="{TemplateBinding BorderBrush}" BorderThickness="0">
<Border Name="Bd" Background="{TemplateBinding Background}" BorderThickness="0">
<DockPanel>
<DockPanel DockPanel.Dock="Top" IsHitTestVisible="False">
<TextBlock x:Name="FieldRequiredInd"
Text="* "
Foreground="Red"
Visibility="Collapsed"
DockPanel.Dock="Left"/>
<TextBlock x:Name="LabelTextBlock"
DockPanel.Dock="Left"
Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=LabelText, Mode=OneWay}" />
</DockPanel>

<ContentPresenter x:Name="CustomContent" DockPanel.Dock="Bottom"
Content="{TemplateBinding Content}"
ContentTemplateSelector="{StaticResource LabeledContentControlDataTemplateSelector}"
MaxHeight="{TemplateBinding MaxHeight}"/>
</DockPanel>
</Border>
</Border>
</Border>
<ControlTemplate.Triggers>
...
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>

模板选择器,也是 super 基本的,如果内容是 string 类型使用 GetType使用字符串的自定义数据模板,以便它们位于文本框中并且可编辑。
<DataTemplate x:Key="StringDataTemplate" DataType="{x:Type system:String}">
<TextBox>
<TextBox.Text>
<Binding Path="." />
</TextBox.Text>
</TextBox>
</DataTemplate>

<local:LabeledContentControlDataTemplateSelector x:Key="LabeledContentControlDataTemplateSelector"
StringDataTemplate="{StaticResource StringDataTemplate}"/>

我如何/可以绑定(bind) LabelTextBoxTestString 正确 这样当我在 StringDataTemplate 生成的文本框中更新/键入内容时它将更新 ViewModel 中的绑定(bind)属性?目前,当我使用第一种方法时,当我输入由 StringDataTemplate 创建的文本框时支持字段不会用新内容更新,但是当使用第二种方法时,支持字段会更新。

最佳答案

您将需要一些修改:

第一个是正确的Binding在您的 TextBox.Text到:

<TextBox.Text>
<Binding Path="Content" RelativeSource="{RelativeSource TemplatedParent}" UpdateSourceTrigger="PropertyChanged" />
</TextBox.Text>

然后更改 ContentPresenter的绑定(bind)到:
<ContentPresenter x:Name="CustomContent"
MaxHeight="{TemplateBinding MaxHeight}"
Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=local:LabeledContentControl}, Path=Content, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ContentTemplateSelector="{StaticResource LabeledContentControlDataTemplateSelector}"
DockPanel.Dock="Bottom" />

现在你可以绑定(bind)你的控件了。小心, Mode设置为 TwoWay对于 Content属性(默认为 OneWay ),这就是您没有得到反馈的原因:
<controls:LabeledContentControl Margin="5"
MaxHeight="25"
LabelText="{Binding LabelString}"
Content="{Binding LabelTextBoxTestString, ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 通过 wpf 中的内容呈现器将公共(public)字符串属性绑定(bind)到 TextBox 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45200056/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com