Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ObjectDataProvider从方法设置文本框中文本的值?_C#_Wpf_Xaml_Binding_Textbox - Fatal编程技术网

C# 如何使用ObjectDataProvider从方法设置文本框中文本的值?

C# 如何使用ObjectDataProvider从方法设置文本框中文本的值?,c#,wpf,xaml,binding,textbox,C#,Wpf,Xaml,Binding,Textbox,背景:我是WPF的新手,几周来一直在努力自学。我有一个在导航窗口中运行的应用程序,只有几页。该页面有5个文本框,其中4个有依赖属性支持。其中三个为times设置了ValidationRules,第四个为double类型设置了ValidationRules。第五个文本框是按钮单击事件的计算输出。该按钮绑定到MultiDataTrigger,在没有验证错误时启用该按钮。巴迪说:“嘿,你已经绑定了所有东西,为什么不更新绑定时的输出框,这样你就不必点击按钮了?” 这似乎是一个好主意,一个很好的武器放在我

背景:我是WPF的新手,几周来一直在努力自学。我有一个在导航窗口中运行的应用程序,只有几页。该页面有5个文本框,其中4个有依赖属性支持。其中三个为times设置了ValidationRules,第四个为double类型设置了ValidationRules。第五个文本框是按钮单击事件的计算输出。该按钮绑定到MultiDataTrigger,在没有验证错误时启用该按钮。巴迪说:“嘿,你已经绑定了所有东西,为什么不更新绑定时的输出框,这样你就不必点击按钮了?”

这似乎是一个好主意,一个很好的武器放在我的wpf工具箱。该按钮有两个用途:计算输出文本框的时间,以及提供使用当前值导航到另一个页面的功能。如果我可以在带有绑定的文本框中显示计算结果,我只需使用按钮导航到下一页。我已经尝试设置一个ObjectDataProvider来与第五个文本框一起使用,这样我就可以调用一个方法来用绑定填充结果。到目前为止,我只成功地导致了许多错误,包括在调用InitializeComponent()的页面上导致stackoverflow

鉴于上述情况,我应该使用textbox.Name属性访问下面的textbox.text,还是从上面的属性或DependencyProperty中获取它

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        DateTime inTime = DateTime.Parse(ttlInTime.Text);
        DateTime outLunch = DateTime.Parse(ttlOutLunch.Text);
        DateTime inLunch = DateTime.Parse(ttlInLunch.Text);
        decimal hours = decimal.Parse(ttlHours.Text);
        //etc.
    }
ObjectDataProvider的方法:

    public string UpdateOutput()
    {
        //do stuff
    }
<ObjectDataProvider ObjectType="{x:Type local:AutoFillBox}" MethodName="UpdateOutput" x:Key="odpOutput">
<ObjectDataProvider.MethodParameters>
    <sys:String>08:00</sys:String>
    <sys:String>12:00</sys:String>
    <sys:String>13:00</sys:String>
    <sys:String>18:00</sys:String>
</ObjectDataProvider.MethodParameters>
一些XAML ObjectDataProvider、其中一个输入文本框和输出文本框:

    <ObjectDataProvider x:Key="outputBox" ObjectType="{x:Type sys:String}" MethodName="UpdateOutput"/>

    <Style x:Key="timeBox3" TargetType="TextBox" BasedOn="{StaticResource tbStyle}">
        <Setter Property="Text">
            <Setter.Value>
                <Binding ElementName="This" Path="timeBox3" UpdateSourceTrigger="
                    <Binding.ValidationRules>
                        <local:TimeValidation/>
                    </Binding.ValidationRules>
                </Binding>
            </Setter.Value>
        </Setter>
    </Style>

    <TextBox Name="ttlInLunch" Style="{StaticResource timeBox3}" Grid.Row="2" Grid.Column="1" TextChanged="TimeBox_TextChanged"
                     GotFocus="TimeBox_GotFocus"/>

    <TextBox Margin="0,2,2,1" Name="ttlOutput" Grid.Row="4" Grid.Column="1" IsReadOnly="True" Background="Transparent" IsTabStop="False"
                     Text="{Binding Source={StaticResource outputBox}}"/>


尝试将您的方法的访问修饰符更改为
public
。目前它是一个
private
方法,因此不能由框架执行

public string UpdateOutput()
{
    //do stuff
}

Bartosz是正确的,我需要定义另一个类来保存我的UpdateOutput方法。还有其他几个因素造成了这种挫折感。首先,我创建了一个类来保存该方法。然后我发现我很难忘记这个类上的默认构造函数。此外,我发现无法将DependencyProperties用作ObjectDataProvider的参数。我删除了整个DependencyProperties集及其各自的绑定。引用这些样式的样式也被删除,验证类的绑定也被删除

//the containing class
public partial class AutoFillBox
{
    public AutoFillBox()
    {
        //dont forget a default constructor
    }

    public string UpdateOutput(string time1, string time2, string time3, string time4)
    {
        //do stuff
    }
}
ObjectDataProvider:

    public string UpdateOutput()
    {
        //do stuff
    }
<ObjectDataProvider ObjectType="{x:Type local:AutoFillBox}" MethodName="UpdateOutput" x:Key="odpOutput">
<ObjectDataProvider.MethodParameters>
    <sys:String>08:00</sys:String>
    <sys:String>12:00</sys:String>
    <sys:String>13:00</sys:String>
    <sys:String>18:00</sys:String>
</ObjectDataProvider.MethodParameters>

08:00
12:00
13:00
18:00

然后简单地将适当的文本框绑定到MethodParameters:

<TextBox Name="recIn" Style="{StaticResource tbStyle}" Grid.Row="1" Grid.Column="1" 
        TextChanged="TimeBox_TextChanged" GotFocus="TimeBox_GotFocus">
<TextBox.Text>
    <Binding Source="{StaticResource odpOutput}" Path="MethodParameters[0]" BindsDirectlyToSource="True" UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <local:TimeValidation/>
        </Binding.ValidationRules>
    </Binding>
</TextBox.Text>

并将方法的输出绑定到textbox控件:

<TextBox Margin="0,2,2,1" Name="recOutput" Grid.Row="5" Grid.Column="1" IsReadOnly="True" Background="Transparent" IsTabStop="False"
        Text="{Binding Source={StaticResource odpOutput}, Mode=OneWay}"/>


No dice,该方法在设置为public(未命中我的断点)后仍然没有被调用。我对这件事感到毛骨悚然,我觉得这很简单。定义UpdateOutput的类的名称是什么?该类应在ObjectType=“{x:Type YOUR_class_NAME}”中指定,所有内容都包含在page类(公共部分类TtlPage:page)中。午饭前我试过了。。。我设置了ObjectType=“{x:Type TtlPage}”。。。它导致了上面描述的堆栈溢出,还完成了杀死VS设计器,并随后杀死了我的VS2013实例。我知道我可以用其他方法来实现它,我只是想学习如何用“WPF”的方法来实现它。你必须把这个方法放在一个单独的类中。巴托斯,我会尽快接受答案。在类中移动该方法会导致我丢失对控件的所有引用,这意味着我也无法执行Validation.GetHasError(控件)以首先确保输入有效。我不确定还有什么其他的检查方法,所以我愿意接受建议。也许我可以将文本框的字符串内容传递给验证方法,但我也不知道该怎么做。我还必须对每个文本框进行多重绑定,因为我已经对每个文本框进行了绑定以进行验证。我们拭目以待。