如何避免文本框中的默认文本?

发布于 2024-10-13 20:12:10 字数 834 浏览 6 评论 0原文

我有一个文本框,它使用多重绑定 usingStringFormat... 如下所示。 但它显示默认值为 {DependencyProperty.UnsetValue},{DependencyProperty.UnsetValue}

如何避免这种情况?

<StackPanel Orientation="Horizontal">
                    <TextBlock Width="70" Text="Name:" Margin="5,2,2,2"></TextBlock>
                    <TextBox Width="160" DataContext="{Binding }" IsReadOnly="True" Margin="2">
                        <TextBox.Text>
                            <MultiBinding StringFormat="{}{0},{1}">
                                <Binding Path="LastName"/>
                                <Binding Path="FirstName"/>
                            </MultiBinding>
                        </TextBox.Text>
                    </TextBox>
</StackPanel>

请帮我。

I have a textbox, which uses multi-binding usingStringFormat...as shown below.
But it displays the default value as
{DependencyProperty.UnsetValue},{DependencyProperty.UnsetValue}

How to avoid this ?

<StackPanel Orientation="Horizontal">
                    <TextBlock Width="70" Text="Name:" Margin="5,2,2,2"></TextBlock>
                    <TextBox Width="160" DataContext="{Binding }" IsReadOnly="True" Margin="2">
                        <TextBox.Text>
                            <MultiBinding StringFormat="{}{0},{1}">
                                <Binding Path="LastName"/>
                                <Binding Path="FirstName"/>
                            </MultiBinding>
                        </TextBox.Text>
                    </TextBox>
</StackPanel>

Please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话里做英雄 2024-10-20 20:12:10

您绑定的对象出现问题。我刚刚从头开始创建了一个应用程序,其中包含继承自 DependencyObject 的 Person 类。我未设置名字和姓氏属性,但没有看到 DependencyProperty.UnsetValue,而是看到一个空白 TextBox,其中仅包含一个逗号。

(一般来说,无论如何,您都不应该在业务对象上使用依赖属性。坚持使用 INotifyPropertyChanged 可以让您省去很多麻烦。)

将代码发布到您的绑定对象,也许我可以发现问题。

public class Person : DependencyObject
{

    public static readonly DependencyProperty FirstNameProperty = DependencyProperty.Register("FirstName", typeof(string), typeof(Person), new FrameworkPropertyMetadata());

    public string FirstName {
        get { return (string)GetValue(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value); }
    }

    public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(Person), new FrameworkPropertyMetadata());

    public string LastName {
        get { return (string)GetValue(LastNameProperty); }
        set { SetValue(LastNameProperty, value); }
    }

}

-

<TextBox IsReadOnly="True">
    <TextBox.Text>
        <MultiBinding StringFormat="{}{1}, {0}">
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

-

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var p = new Person();
        //p.FirstName = "Josh";
        //p.LastName = "Einstein";          
        DataContext = p;
    }
}

Something is wrong with the object you are binding to. I just created an application from scratch with a Person class that inherits from DependencyObject. I left the first and last name properties unset and I did not see DependencyProperty.UnsetValue, but rather a blank TextBox with just a comma in it.

(In general, you shouldn't use dependency properties on your business objects anyway. Stick to INotifyPropertyChanged and save yourself a ton of headaches.)

Post the code to your bound object and maybe I can spot the issue.

public class Person : DependencyObject
{

    public static readonly DependencyProperty FirstNameProperty = DependencyProperty.Register("FirstName", typeof(string), typeof(Person), new FrameworkPropertyMetadata());

    public string FirstName {
        get { return (string)GetValue(FirstNameProperty); }
        set { SetValue(FirstNameProperty, value); }
    }

    public static readonly DependencyProperty LastNameProperty = DependencyProperty.Register("LastName", typeof(string), typeof(Person), new FrameworkPropertyMetadata());

    public string LastName {
        get { return (string)GetValue(LastNameProperty); }
        set { SetValue(LastNameProperty, value); }
    }

}

-

<TextBox IsReadOnly="True">
    <TextBox.Text>
        <MultiBinding StringFormat="{}{1}, {0}">
            <Binding Path="FirstName" />
            <Binding Path="LastName" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>

-

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
    }

    private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        var p = new Person();
        //p.FirstName = "Josh";
        //p.LastName = "Einstein";          
        DataContext = p;
    }
}
伤痕我心 2024-10-20 20:12:10

只需给出fallbackvalue =“”并查看

<TextBox.Text>
                <MultiBinding StringFormat="{}{0},{1}">
                    <Binding Path="LastName" FallbackValue=""/>
                    <Binding Path="FirstName" FallbackValue=""/>
                </MultiBinding>
            </TextBox.Text>

如果绑定不成功,即未找到绑定源的路径或值转换器(如果有)失败,则返回DependencyProperty.UnsetValue,然后将目标属性设置为FallbackValue(如果您当然定义了一个)。

just give the fallbackvalue="" and see

<TextBox.Text>
                <MultiBinding StringFormat="{}{0},{1}">
                    <Binding Path="LastName" FallbackValue=""/>
                    <Binding Path="FirstName" FallbackValue=""/>
                </MultiBinding>
            </TextBox.Text>

If a binding is not successful, i.e. the path to the binding source is not found or the value converter , if any, fails, a DependencyProperty.UnsetValue is returned, then the target property is set to the FallbackValue, if you defined one of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文