1

在初始页面加载时,我正在设置它以便表单准备好输入新记录。对于一些自定义数据验证器,我将绑定设置为自身。我的问题是如何将默认文本设置为?

<TextBox>
    <TextBox.Text>
        <Binding RelativeSource="{RelativeSource Self}"
                 Path="Text"
                 UpdateSourceTrigger="LostFocus" >
            <Binding.ValidationRules>
                <validators:MyCustomValidators />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
4

1 回答 1

4

Add an event handler on the Loaded or Initialized event, and set the Text there.

<TextBox Loaded="TextBox_Loaded_1">
    <TextBox.Text>
        <Binding RelativeSource="{RelativeSource Self}"
                 Path="Text"
                 UpdateSourceTrigger="LostFocus" >
            <Binding.ValidationRules>
                <validators:MyCustomValidators />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

And in the code behind :

private void TextBox_Loaded_1(object sender, RoutedEventArgs e)
{
    ((TextBox)sender).Text = "Default text";
}

EDIT:

XAML only solution :

<TextBox>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Text" Value="Default text" />
        </Style>
    </TextBox.Style>
    <TextBox.Text>
        <Binding RelativeSource="{RelativeSource Self}"
                 Path="Text"
                 UpdateSourceTrigger="LostFocus" >
            <Binding.ValidationRules>
                <validators:MyCustomValidators />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
于 2012-11-15T21:13:57.073 回答