首页 新闻 赞助 找找看

wpf 子元素的binding失效问题

0
悬赏园豆:15 [已解决问题] 解决于 2014-06-19 10:13
 1    <Window.Resources>
 2         <Style x:Key="tb">
 3             <Style.Setters>
 4                 
 5             </Style.Setters>
 6             <Style.Triggers>
 7                 <Trigger Property="Control.IsFocused" Value="True">
 8                     <Setter Property="Control.Background" Value="White"></Setter>
 9                 </Trigger>
10             </Style.Triggers>
11         </Style>
12     </Window.Resources>
13 
14 <TextBox Background="Red" Style="{StaticResource ResourceKey=tb}"></TextBox>

用TextBox的style绑定资源的 tb没有问题 当鼠标的焦点在textbox上,背景色是白色的,失去焦点 变成红色。

当是:如果TextBox被潜嵌在两层Grid中时  绑定就不起作用了 不知道为什么。。。。

二线中的嗷嗷者的主页 二线中的嗷嗷者 | 初学一级 | 园豆:14
提问于:2014-06-18 14:39
< >
分享
最佳答案
1

纠正下,{StaticResource ResourceKey=tb} 这个不是绑定。。。 {Binding xxxx} ,才是Binding.

Style={StaticResource ResourceKey=tb} 是把静态资源“tb” 赋值给TextBox.Style属性。跟binding完全是两个概念,作用也很大不同。

<TextBox Background="Red" Style="{StaticResource ResourceKey=tb}"></TextBox> 你这种写法放哪里都起不了作用的。因为涉及到优先级的问题。<TextBox Background="Red"  这里的直接赋值 优先级要高于Style中的Setter中的赋值。

优先级:Template的值>控件中属性值>Style.Triggers中的值>Style.Setter中的值。(这个只是通常情况,如果模板样式定义的比较特殊,优先级可能不同)

这样可以实现你的需求

<Window.Resources>
        <Style x:Key="tb">
            <Setter Property="Control.Background" Value="Red"/>
            <Style.Triggers>
                <Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="Control.Background" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox  Style="{StaticResource tb}" Height="30" Width="100"/>
    </Grid>
收获园豆:15
德年 | 小虾三级 |园豆:810 | 2014-06-18 15:20

你讲的很好 但是 没有解决 我的问题 

我 是想表达 为什么 嵌套两层grid中的textbox 利用了 staticresource 的触发器后 不起作用 (得到焦点 背景色不会改变) 

二线中的嗷嗷者 | 园豆:14 (初学一级) | 2014-06-18 16:16

@我想成为技术大咖: 我说了,是优先级的问题,触发器的优先级低,所以不起作用。

另外,只要设置了<TextBox Background="Red" />,不只是在嵌套两层Grid中才不起作用,而是所有地方都不起作用。 

你不信,可以测试这个代码

<Window.Resources>
        <Style x:Key="tb">
            <Style.Triggers>
                <Trigger Property="Control.IsFocused" Value="True">
                    <Setter Property="Control.Background" Value="White"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <TextBox Background="Red" Style="{StaticResource tb}" Height="30" Width="100"/>

这个是没有效果的。

德年 | 园豆:810 (小虾三级) | 2014-06-18 17:14

@德年: 大神终于点醒了我 多谢 是优先级的问题 我按照你说的 没有赋属性值 都是通过 trigger 实现了 想要的 效果 多谢!!!

二线中的嗷嗷者 | 园豆:14 (初学一级) | 2014-06-19 10:12

@德年: 大神 能请教另外一个问题 吗  就是wpf里 progressbar 进度条 进度 的显示问题

view中的progressbar的value绑定了 viewmodel的showprogress 然后 viewmodel中有一方法(异步的) 给showprogress赋值 运行时 progressbar进度条不显示 到最后 100%时才显示 请问 是什么情况。

 1         public int showProgress;
 2         public int ShowProgress
 3         {
 4             get { return showProgress; }
 5             set
 6             {
 7                 if (showProgress != value)
 8                 {
 9                     showProgress = value;
10                     RaisePropertyChanged("ShowProgress");
11                 }
12             }
13         }
14 
15 
16         public ProgressBar progressBar;
17         public DelegateCommand<ProgressBar> LoadedCommand { get; set; }
18         public DownloadPptViewModel()
19         {
20             LoadedCommand = new DelegateCommand<ProgressBar>(new Action<ProgressBar>(Loaded));
21 
22         }
23 
24         public void Loaded(ProgressBar pb)
25         {
26             progressBar = pb;
27             WebClient wc = new WebClient();
28             wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged);
29             wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted);
30 
31             wc.DownloadFileAsync(new Uri(ConfigurationManager.AppSettings["TestPptPath"]), System.Environment.CurrentDirectory + "\\temp.ppt");
32             wc.Dispose();
33         }
34         void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
35         {
36 
37         }
38 
39         void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
40         {
41             ShowProgress = e.ProgressPercentage;
42         }
二线中的嗷嗷者 | 园豆:14 (初学一级) | 2014-06-19 10:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册