简体   繁体   中英

Disabled TextBox Style

I have created a TextBox above a Path-Element (the Path Element draws a rectangular thing, which acts as the design of the textbox). Now I want to disable this TextBox with

valCon.ValueTextBox.IsEnabled = false;

This works so far. However, since I don't want the TextBox to have any Style (no Color, no Borders, etc.), but only a visible Text in it, I'm getting a small problem:

When the TextBox is disabled, it automatically receives a style which I can't get rid of. The Background changes to white, the Opacity changes to around 0.3 and Borders appear.

I can't seem to solve this problem by adding

valcon.ValueTextBox.Background = new SolidColorBrush(Colors.Transparent);
valcon.ValueTextBox.BorderBrush = new SolidColorBrush(Colors.Transparent); 

etc.

It just seems to ignore this. Does anyone know a solution?

Greetings Narakuvera

You need to take control over the template to achieve the same

here is a basic template for you with no border and no background TextBox

    <TextBox Text="hello">
        <TextBox.Template>
            <ControlTemplate TargetType="TextBox">
                <ScrollViewer Margin="0"
                              x:Name="PART_ContentHost" />
            </ControlTemplate>
        </TextBox.Template>
    </TextBox>

you can choose to set IsEnabled="False" and it will still remain border less

Code behind approach

        ControlTemplate ct = new ControlTemplate(typeof(TextBox));
        FrameworkElementFactory sv = new FrameworkElementFactory(typeof(ScrollViewer));
        sv.Name = "PART_ContentHost";
        ct.VisualTree = sv;
        textbox1.Template = ct;

WinRT code behind approach

        string template = "<ControlTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TargetType=\"TextBox\"><ScrollViewer Name=\"PART_ContentHost\" /></ControlTemplate>";
        ControlTemplate сt = (ControlTemplate)XamlReader.Load(template);
        textbox1.Template = сt;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM