使用资源字典无法获取textbox.text [英] can't get textbox.text using resource dictionary

查看:58
本文介绍了使用资源字典无法获取textbox.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在玩wpf和资源字典.
当我有一个简单的表单,上面有2个按钮和2个文本框,而没有使用字典中的模板(Custom_Controls.xaml)时,我可以轻松获取文本值.

应用模板后,我得到"返回.

如果有人可以帮助的话,将不胜感激...

xaml窗口:

<window x:class="eHandover.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <grid>
        <image name="image1" stretch="Fill" source="bkgrnd_ppl.jpg" opacity="0.5" />
        <button height="27" margin="103,0,100,45" name="button1" verticalalignment="Bottom" template="{StaticResource buttonTemplate}" foreground="DarkBlue" click="button1_Click">Log On</button>
        <button height="27" margin="103,0,100,12" name="button2" verticalalignment="Bottom" template="{StaticResource buttonTemplate}" foreground="DarkBlue" click="button2_Click">Close</button>
        <textbox margin="79,122,79,117" name="textBox1" verticalalignment="Bottom" template="{StaticResource textTemplate}" width="120" background="White" foreground="Black" textchanged="textBox1_TextChanged" />
        <passwordbox height="23" margin="79,0,79,88" name="passwordBox1" verticalalignment="Bottom" template="{StaticResource PasstextTemplate}" />
    </grid>
</window>



窗口代码:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            
            MessageBox.Show(this.textBox1.Text.ToString());
            
        
        }



资源字典:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <controltemplate x:key="buttonTemplate" targettype="{x:Type Button}" xmlns:x="#unknown">
        <grid>
            <ellipse x:name="outerCircle">
                <ellipse.fill>
                    <lineargradientbrush startpoint="0,0" endpoint="0,1">
                        <gradientstop offset="0" color="Blue" />
                        <gradientstop offset="1" color="AliceBlue" />
                    </lineargradientbrush>
                </ellipse.fill>
            </ellipse>
            <ellipse x:name="innerCircle" rendertransformorigin=".5,.5">
                <ellipse.rendertransform>
                    <scaletransform scalex=".8" scaley=".8" />
                </ellipse.rendertransform>
                <ellipse.fill>
                    <lineargradientbrush startpoint="0,0" endpoint="0,1">
                        <gradientstop offset="0" color="AliceBlue" />
                        <gradientstop offset="1" color="Blue" />
                    </lineargradientbrush>
                </ellipse.fill>
            </ellipse>
            <viewbox>
                <contentpresenter margin="{TemplateBinding Padding}" />
            </viewbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter targetname="outerCircle" property="Fill" value="AliceBlue" />
            </trigger>
            <trigger property="IsPressed" value="True">
                <setter property="RenderTransform">
                    <setter.value>
                        <scaletransform scalex=".8" scaley=".8" />
                    </setter.value>
                </setter>
                <setter property="RenderTransformOrigin" value=".5,.5" />
            </trigger>
        </controltemplate.triggers>
    </controltemplate>

    <controltemplate x:key="textTemplate" targettype="{x:Type TextBox}" xmlns:x="#unknown">
        <grid>
            <textbox width="120" opacity="0.7"></textbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="DeepPink" glowsize="10" noise="0.4">
               Opacity="0.4" />
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
            <trigger property="IsFocused" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="AliceBlue" glowsize="10" noise="0.4">
               Opacity="0.4"/>
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
        </controltemplate.triggers>
    </controltemplate>
    
    <controltemplate x:key="PasstextTemplate" targettype="{x:Type PasswordBox}" xmlns:x="#unknown">
        <grid>
            <passwordbox width="120" opacity="0.7"></passwordbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="WhiteSmoke" glowsize="10" noise="0.4">
               Opacity="0.4" />
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
        </controltemplate.triggers>
    </controltemplate>
    
</resourcedictionary>


App.xaml:

<application x:class="eHandover.App" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <application.resources>
            <resourcedictionary source="Custom_Controls.xaml" />
    </application.resources>
</application>

解决方案

没有任何世俗的理由要求您的模板更改代码的工作方式.您在字符串上调用"ToString"的事实使我想知道您是否完全了解C#.我建议您删除模板的各个方面,直到模板为空为止,然后查看代码是否在任何时候都能开始工作,然后让我们确切地知道是什么代码导致了问题.


Hi,

I having a play with wpf and resource dictionaries.
When I have a simple form with 2 buttons on and 2 textboxes without using the templates in the dictionary (Custom_Controls.xaml) I can easily get the text value.

When the templates are applied I get "" returned.

If anybody could help it''d be much appreciated...

window xaml:

<window x:class="eHandover.Window1" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <grid>
        <image name="image1" stretch="Fill" source="bkgrnd_ppl.jpg" opacity="0.5" />
        <button height="27" margin="103,0,100,45" name="button1" verticalalignment="Bottom" template="{StaticResource buttonTemplate}" foreground="DarkBlue" click="button1_Click">Log On</button>
        <button height="27" margin="103,0,100,12" name="button2" verticalalignment="Bottom" template="{StaticResource buttonTemplate}" foreground="DarkBlue" click="button2_Click">Close</button>
        <textbox margin="79,122,79,117" name="textBox1" verticalalignment="Bottom" template="{StaticResource textTemplate}" width="120" background="White" foreground="Black" textchanged="textBox1_TextChanged" />
        <passwordbox height="23" margin="79,0,79,88" name="passwordBox1" verticalalignment="Bottom" template="{StaticResource PasstextTemplate}" />
    </grid>
</window>



window code:

private void button1_Click(object sender, RoutedEventArgs e)
        {
            
            MessageBox.Show(this.textBox1.Text.ToString());
            
        
        }



resource dictionary:

<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <controltemplate x:key="buttonTemplate" targettype="{x:Type Button}" xmlns:x="#unknown">
        <grid>
            <ellipse x:name="outerCircle">
                <ellipse.fill>
                    <lineargradientbrush startpoint="0,0" endpoint="0,1">
                        <gradientstop offset="0" color="Blue" />
                        <gradientstop offset="1" color="AliceBlue" />
                    </lineargradientbrush>
                </ellipse.fill>
            </ellipse>
            <ellipse x:name="innerCircle" rendertransformorigin=".5,.5">
                <ellipse.rendertransform>
                    <scaletransform scalex=".8" scaley=".8" />
                </ellipse.rendertransform>
                <ellipse.fill>
                    <lineargradientbrush startpoint="0,0" endpoint="0,1">
                        <gradientstop offset="0" color="AliceBlue" />
                        <gradientstop offset="1" color="Blue" />
                    </lineargradientbrush>
                </ellipse.fill>
            </ellipse>
            <viewbox>
                <contentpresenter margin="{TemplateBinding Padding}" />
            </viewbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter targetname="outerCircle" property="Fill" value="AliceBlue" />
            </trigger>
            <trigger property="IsPressed" value="True">
                <setter property="RenderTransform">
                    <setter.value>
                        <scaletransform scalex=".8" scaley=".8" />
                    </setter.value>
                </setter>
                <setter property="RenderTransformOrigin" value=".5,.5" />
            </trigger>
        </controltemplate.triggers>
    </controltemplate>

    <controltemplate x:key="textTemplate" targettype="{x:Type TextBox}" xmlns:x="#unknown">
        <grid>
            <textbox width="120" opacity="0.7"></textbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="DeepPink" glowsize="10" noise="0.4">
               Opacity="0.4" />
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
            <trigger property="IsFocused" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="AliceBlue" glowsize="10" noise="0.4">
               Opacity="0.4"/>
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
        </controltemplate.triggers>
    </controltemplate>
    
    <controltemplate x:key="PasstextTemplate" targettype="{x:Type PasswordBox}" xmlns:x="#unknown">
        <grid>
            <passwordbox width="120" opacity="0.7"></passwordbox>
        </grid>
        <controltemplate.triggers>
            <trigger property="IsMouseOver" value="True">
                <setter property="BitmapEffect">
                    <setter.value>

                        <!-- The OuterGlow is blue, extends out 30 pixels, has the 
                   maximum noise possible, and is 40% Opaque. -->
                        <outerglowbitmapeffect glowcolor="WhiteSmoke" glowsize="10" noise="0.4">
               Opacity="0.4" />
                    </outerglowbitmapeffect></setter.value>
                </setter>
            </trigger>
        </controltemplate.triggers>
    </controltemplate>
    
</resourcedictionary>


App.xaml:

<application x:class="eHandover.App" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    StartupUri="Window1.xaml">
    <application.resources>
            <resourcedictionary source="Custom_Controls.xaml" />
    </application.resources>
</application>

解决方案

There is no earthly reason for your template to change how your code works. The fact that you call ''ToString'' on a string makes me wonder if you know much C# at all. I''d suggest removing aspects of your template until it''s empty and see if the code starts to work at any point, then let us know exactly what code is causing the issue.


这篇关于使用资源字典无法获取textbox.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆