C# 如何设置文本框';以编程方式或从滑块选择宽度

C# 如何设置文本框';以编程方式或从滑块选择宽度,c#,xaml,windows-store-apps,C#,Xaml,Windows Store Apps,我试图在C#中以编程方式编辑文本框Width属性,但最终最好的行为应该是在打开应用程序时将该值默认为1000,然后使用滑块进行修改 这是我用来为Width属性设置数据绑定的代码 <TextBox x:Name="MainTextArea" Margin="0,71" BorderThickness="0" Background="{x:Null}" BorderBrush="{x:Null}"

我试图在C#中以编程方式编辑
文本框
Width属性,但最终最好的行为应该是在打开应用程序时将该值默认为1000,然后使用
滑块
进行修改

这是我用来为
Width
属性设置数据绑定的代码

<TextBox x:Name="MainTextArea"
         Margin="0,71" 
         BorderThickness="0" 
         Background="{x:Null}" 
         BorderBrush="{x:Null}"        
         SelectionHighlightColor="#FF444444" 
         Foreground="White" 
         Canvas.ZIndex="1"
         TabIndex="1" 
         Padding="0" 
         FontSize="18" 
         FontFamily="Georgia" 
         IsSpellCheckEnabled="False" 
         HorizontalAlignment="Center" 
         RequestedTheme="Dark" 
         Width="{Binding TextAreaWidth}" 
         TextChanged="MainTextArea_TextChanged" 
         TextWrapping="Wrap"
         AcceptsReturn="True" 
         PlaceholderText=""
         />
然后我尝试在MainPage类中设置它

public MainPage()
{
    this.InitializeComponent();
    // Create an instance of the MyWidth class 
    // that implements INotifyPropertyChanged.
    MyWidth mywidth = new MyWidth();

    mywidth.width = new FrameworkElement;  // this line is not recognized, I suppose it is incomplete, but I don't know what to write there.
    MainTextArea.DataContext = mywidth;

    // Create the binding and associate it with the text box.
    Binding binding = new Binding() { Path = new PropertyPath("TextAreaWidth") };
    MainTextArea.SetBinding(TextBox.WidthProperty, binding);
}
目前
mywidth.width=新的框架元素未被识别为有效行,我认为它不完整,但我不知道在那里写什么。在这个示例中,创建了一个新的SolidColorBrush,并使用它将颜色设置为红色,但我不知道如何在框架元素示例中使用它

要使用滑块,我想我需要在滑块的
ValueChanged
事件中调用其中一个类

帮忙

编辑和替换解决方案 @BryanStump的回答是正确的,并且确实解决了我的问题,因为我得到了一个
NullReferenceException
错误

然而,当我试图自己修复它时,我学会了如何正确地将
滑块的值绑定到
文本框的宽度属性,如下所示:

<TextBox x:Name="MainTextArea" 
         Margin="0,71" 
         BorderThickness="0" 
         Background="{x:Null}" 
         BorderBrush="{x:Null}" 
         SelectionHighlightColor="#FF444444" 
         Foreground="White" 
         Canvas.ZIndex="1" 
         TabIndex="1" 
         ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
         Padding="0" 
         FontSize="18" 
         FontFamily="Georgia" 
         IsSpellCheckEnabled="False" 
         HorizontalAlignment="Center" 
         RequestedTheme="Dark" 
         Width="{Binding Value, ElementName=TextBoxSizeSlider}"   
         TextChanged="MainTextArea_TextChanged" 
         TextWrapping="Wrap" 
         AcceptsReturn="True" 
         PlaceholderText="" Loaded="MainTextArea_Loaded" />


请注意宽度是如何绑定到
滑块的值的。

如果要在滑块
值更改
事件上设置
文本框的宽度,那么为什么不直接在事件处理程序中执行此操作

private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
{
    textBox.Width = e.newValue;
}
然后可以在XAML中设置默认值1000。不需要绑定

编辑:

关于
NullReferenceException
,请查看以下问题:

它注意到,一旦创建滑块,它将引发
ValueChanged
事件,根据XAML的顺序,该事件可能会导致问题


它提到了最简单的修复方法是在
ValueChanged
事件中捕获null对象,但更好的替代方法是在XAML中将值绑定在一起(而不是在后面的代码中)。

文本框为null,因为它尚未加载

    private void Slider_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        if (TextBox != null)
        {
            TextBox.Width = e.NewValue;
        }
    }
如果在滑块值更改后加载textbox,它将丢失初始值


它不起作用,我得到此NullReferenceException,该特定行的文本如下:在(应用程序名称)中发生了类型为“System.NullReferenceException”的首次意外异常。其他信息:对象引用未设置为对象的实例。我以前用一个更简单的行尝试过同样的方法,比如:textBox.Width=slider.Value;但我得到了同样的结果。你在检查textBox是否为空吗?滑块在文本框之前加载。当UI加载时,可以在加载文本框之前设置滑块值。如果是这种情况,请为TextBox.Loaded=“your event handler”创建一个事件处理程序,并在其中设置值。@BryanStump,我尝试了一些操作,但它仍然在值更改事件中卡住。请参见编辑代码。加载页面时,为什么需要设置
文本框的宽度?您没有说过,在用户可以操作滑块之前,该值可能会发生变化,因此默认情况下,它将只是您在XAML中设置的长度(我假设您已将其设置为
滑块的默认值
?或者,如果您真的想在页面加载时设置宽度,请在页面构造函数中的
InitialiseComponent
之后进行设置。@J.B我不明白您的意思。我不会在页面加载时设置它,正如您已经建议的那样。默认宽度值已设置到1100。滑块值也从XAML设置为1100。现在,正如我试图解释的那样,我想通过使用
滑块
来更改
文本框
的宽度。撇开我尝试的绑定方法和您的建议不谈,如果我尝试从
滑块
的ValueChanged事件更改宽度,我会得到'System.nullReference'(应用程序名称)附加信息中出现ceException:“对象引用未设置为对象的实例。我前面提到的错误。尽管应调用正确的事件名称:`Slider_ValueChanged(…)和TextBox.Loaded(…)
    private void Slider_OnValueChanged(object sender, RangeBaseValueChangedEventArgs e)
    {
        if (TextBox != null)
        {
            TextBox.Width = e.NewValue;
        }
    }
    private void TextBox_OnLoaded(object sender, RoutedEventArgs e)
    {
        if (Slider != null)
        {
            TextBox.Width = Slider.Value;
        }
    }