相关疑难解决方法(0)

使用MEF导入WPF DataTemplate?

我把MEF看成是一个可扩展性框架,除了一点之外我几乎卖得很多:

假设我要导入ViewModel和View来显示它.我认为这样做的"正确"方法是MEF部分导出ViewModel类,以及显示ViewModel的DataTemplate.例如,假设您正在构建类似Visio的应用程序,并且您想要导入形状库.每个形状都需要在Xaml中定义的View和一个将包装一些底层Model对象的ViewModel.

这可能吗?DataTemplate的Import合约会是什么样的?如何让WPF知道导入的DataTemplate?

wpf mef datatemplate mvvm

24
推荐指数
1
解决办法
6792
查看次数

为什么在将Label.Content绑定到非字符串而不是字符串时会应用隐式TextBlock样式?

我正在查看这个问题,并发现绑定Label.Content到非字符串值将应用隐式TextBlock样式,但是绑定到字符串则不会.

以下是重现问题的示例代码:

<Window.Resources>
    <Style TargetType="Label">
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Margin" Value="10"/>
        <Setter Property="VerticalAlignment" Value="Center"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="FontSize" Value="26"/>
        <Setter Property="Margin" Value="10"/>
    </Style>
</Window.Resources>

<Grid>
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding SomeString}" Background="Red"/>
        <Label Content="{Binding SomeDecimal}" Background="Green"/>
    </StackPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)

绑定值的代码是

SomeDecimal = 50;
SomeString = SomeDecimal.ToString();
Run Code Online (Sandbox Code Playgroud)

最终结果如下所示,Margin隐式TextBlock样式的属性应用于仅绑定到非字符串的Label:

在此输入图像描述

两个标签都呈现为

<Label>
    <Border>
        <ContentPresenter>
            <TextBlock />
        </ContentPresenter>
    </Border>
</Label>
Run Code Online (Sandbox Code Playgroud)

当我使用Snoop检查VisualTree时,我可以看到它对于两个元素看起来完全相同,除了第二个TextBlock从隐式样式应用Margin,而第一个没有.

在此输入图像描述

我已经使用Blend来提取默认标签模板的副本,但是没有看到任何奇怪的东西,当我将模板应用于我的两个标签时,同样的事情发生了.

<Label.Template>
    <ControlTemplate TargetType="{x:Type Label}">
        <Border BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                Background="{TemplateBinding Background}" 
                Padding="{TemplateBinding …
Run Code Online (Sandbox Code Playgroud)

wpf label styles implicit-style

18
推荐指数
1
解决办法
1738
查看次数

为什么我的TextBlock/TextBox不能应用基本样式的值?

对于我来说,为数据输入表单的样式编写类似的东西并不罕见,但我的问题是,TextBox并且TextBlock似乎没有实现在中的Setter BaseElementStyle.通常我需要单独定义它们.

为什么是这样?它有办法吗?

我猜它与其他控件模板中常用的事实有关(例如,TextBlock用于大多数控件,TextBox用于DatePickers和ComboBoxes)

<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}">
    <Setter Property="Margin" Value="5" />
    <Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" />
Run Code Online (Sandbox Code Playgroud)

wpf inheritance xaml coding-style basedon

5
推荐指数
2
解决办法
3201
查看次数

覆盖ComboBox中的默认TextBlock样式

我在App.xaml中定义了默认的TextBlock样式,这似乎也会影响ComboBox项的文本颜色。现在,如何在主窗口中显式设置ComboBox的文本颜色?(我想保留默认样式,但组合框的文本颜色为蓝色而不是红色...)

应用程式

<Application x:Class="WpfApplication1.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Grid>
    <ComboBox Name="comboBox1" SelectedIndex="0" HorizontalAlignment="Left" VerticalAlignment="Top">
        <ComboBoxItem Content = "Item1"/>
        <ComboBoxItem Content = "Item2"/>
        <ComboBoxItem Content = "Item3"/>
    </ComboBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

  1. 设置组合框。前景
  2. 设置TextElement.Foreground
  3. 设置TextBlock.Foreground
  4. 在ComboBox.Resources中定义另一个隐式TextBlock样式
  5. 在Grid.Resources中定义另一个隐式TextBlock样式
  6. 在Window.Resources中定义另一个隐式TextBlock样式

c# wpf

3
推荐指数
1
解决办法
2890
查看次数

覆盖嵌套控件中的隐式样式

我的应用程序中有一个资源字典,其中它们是为textblock定义的通用样式,此字典与app.xaml合并.

现在我有一个要求,我需要在对话框窗口中更改tabitem的样式,并根据几个触发器设置前景,我已经为tabitem和textblock定义了我自己的文本块样式和书面模板,如下所示 -

/* wriiten in common style */
       <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Foreground" Value="{StaticResource BR_SE_Black}" />
                <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
                <Setter Property="FontSize" Value="11"/>
                <Setter Property="FontFamily" Value="Arial Unicode MS"/>
                <Style.Triggers> 
                    <Trigger Property="controls:TextBlockService.IsTextTrimmed" Value="True">
                        <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                    </Trigger>
                </Style.Triggers>
            </Style>




   /* written in dialog winow */
 <Style TargetType="{x:Type TextBlock}">
            <Setter Property="VerticalAlignment" Value="Center"></Setter>
            <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
            <Setter Property="FontSize" Value="11"/>
            <Setter Property="FontFamily" Value="Arial Unicode MS"/>
            <Style.Triggers>
                <Trigger Property="Controls:TextBlockService.IsTextTrimmed" Value="True">
                    <Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style  TargetType="{x:Type TabControl}"> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml

3
推荐指数
1
解决办法
1483
查看次数