欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf 修改style

程序员文章站 2022-03-07 14:25:42
...

最近有个奇怪的需求,希望能动态的修改DataTrigger,试了一下,没做出来,最后是通过新建了额外的一个Style解决,记录如下,希望在DataTrigger里面动态增加一个Trigger。

  <Style TargetType="{x:Type ContentControl}" x:Key="mainContentAStyle">
        <Style.Triggers x:Uid="triggerUid">
            <DataTrigger Binding="{Binding MainViewType}" Value="ControlA">
                <Setter Property="Content" >
                    <Setter.Value>
                        <Grid>
                            <my:ControlA />
                        </Grid>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

刚开始实现思路是

private void ChangeStyle()
        {
            MainWndViewModel vm = this.DataContext as MainWndViewModel;
            if (vm != null)
            {
                Style contentStyle = mainContent.Style;
                DataTrigger newDataTrigger = new DataTrigger();
                Binding dataBinding = new Binding()
                {
                    Source = vm,
                    Path = new PropertyPath("MainViewType"),
                    UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                };
                ControlB controlB = new ControlB();
                newDataTrigger.SetValue(ContentControl.ContentProperty, controlB);
                Collection<TriggerBase> triggerCollection = new Collection<TriggerBase>();
                foreach (var item in contentStyle.Triggers)
                {
                    triggerCollection.Add(item);
                }
                triggerCollection.Add(newDataTrigger);
                //mainContent.Style.UnregisterName("triggerUid");
                //mainContent.Style.RegisterName("triggerUid", triggerCollection);
                //contentStyle.Triggers = triggerCollection;
                contentStyle.Triggers.Add(newDataTrigger);
            }
        }

发现会抛异常 “使用“TriggerCollection”之后(密封),无法对其进行修改。”

最后只好通过新建一个Style,动态切换style实现

  <Style TargetType="{x:Type ContentControl}" x:Key="mainContentBStyle">
        <Style.Triggers  >
            <DataTrigger Binding="{Binding MainViewType}" Value="ControlA">
                <Setter Property="Content" >
                    <Setter.Value>
                        <Grid>
                            <my:ControlA />
                        </Grid>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding MainViewType}" Value="ControlB">
                <Setter Property="Content" >
                    <Setter.Value>
                        <Grid>
                            <my:ControlB Focusable="False" />
                        </Grid>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>

切换

        private void btnChangeStyle_Click(object sender, RoutedEventArgs e)
        {
            MainWndViewModel vm = this.DataContext as MainWndViewModel;
            if (vm != null)
            {
                if (vm.MainViewType == MainViewType.ControlA)
                {
                    vm.MainViewType = MainViewType.ControlB;
                    mainContent.SetValue(ContentControl.StyleProperty, Application.Current.Resources["mainContentBStyle"]);
                }
                else
                {
                    vm.MainViewType = MainViewType.ControlA;
                    mainContent.SetValue(ContentControl.StyleProperty, Application.Current.Resources["mainContentAStyle"]);
                }
            }
        }
另,切换style时,ControlB仅初始化了一次。

代码

https://download.csdn.net/download/bornonew/10429724