WPF简单的提示和技巧?

Bra*_*rad 19 wpf xaml

我正在利用边距和填充进行愚弄,发现负值是可以接受的,并且在适当的情况下会产生很好的效果.例如,如果您有一个带有填充对象的边框,并且您希望填充的对象颜色超出边框.有没有人?

Ari*_*lBH 19

调试WPF绑定.

添加绑定属性的跟踪:

<Window …
 xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"/>
    <TextBlock Text="{Binding Path=Caption, 
diagnostics:PresentationTraceSources.TraceLevel=High}"…/>
Run Code Online (Sandbox Code Playgroud)

您将在输出窗口中获得有关绑定的更多详细信息:

PropertyChanged event from SomeObject (hash=1)
SetValue at level 0 from SomeObject (hash= 1) using RuntimePropertyInfo(Field): 
'False'
TransferValue - got raw value 'False'
TransferValue - using final value 'False'
Run Code Online (Sandbox Code Playgroud)

// 在这里编辑更多信息.

阿里尔


Ari*_*lBH 12

与3.5 SP1一起提供的WPF的新功能是能够在绑定时格式化字符串.它消除了IValueConverter这种常见情况的使用.这里有一些例子可以让你从这篇博文中复制过来

<TextBox Text="{Binding Path=Double, StringFormat=F3}"/>
<TextBox Text="{Binding Path=Double, StringFormat=Amount: {0:C}}"/>
<TextBox Text="{Binding Path=Double, StringFormat=Amount: \{0:C\}}"/>
<TextBox>
  <TextBox.Text>
    <Binding Path="Double" StringFormat="{}{0:C}"/>
  </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)


Bra*_*rad 10

Visibility是一个三态System.Windows.Visibility枚举:

  • 可见 - 元素被渲染并参与布局.
  • 折叠 - 元素不可见,不参与布局.有效地给它一个0的高度和宽度,并表现得好像它不存在.
  • 隐藏 - 元素不可见但继续参与布局.


Bra*_*rad 7

设置提供可视提示的调试样式:

<Window.Resources>

  <Style x:Key="DebugGrid" TargetType="Grid">
    <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="ShowGridLines" Value="True"/>
      </Trigger>
    </Style.Triggers>
  </Style>

</Window.Resources>

<Grid Name="Grid"
      Style="{StaticResource DebugGrid}"
      Background="Black">...
Run Code Online (Sandbox Code Playgroud)

  • 啊,明确节省时间.. (2认同)

Bra*_*rad 5

在控件的内容中包含花括号.

<Button Content="{}{This is not a markup extension.}"/>
Run Code Online (Sandbox Code Playgroud)


Bra*_*rad 5

IsMouseOver并且IsMouseDirectlyOver是不同的事件. IsMouseOver响应控件内的所有鼠标移动及其子项.IsMouseDirectlyOver仅当光标位于控件本身上时才响应.例如,如果边框中包含标签,则IsMouseDirectlyOver只有当光标位于Border本身上方但未覆盖包含的Label时,才会触发Border 的事件.


Bra*_*rad 3

计算可用房地产的百分比:

<Grid.RowDefinitions>
  <RowDefinition Height="0.25*"/>
  <RowDefinition Height="0.25*"/>
  <RowDefinition Height="0.25*"/>
  <RowDefinition Height="0.25*"/>
</Grid.RowDefinitions>
Run Code Online (Sandbox Code Playgroud)

编辑:

这可以工作,但并不表示 * 参数如何起作用。这:

<Grid.RowDefinitions>
  <RowDefinition Height="*"/>
  <RowDefinition Height="*"/>
  <RowDefinition Height="*"/>
  <RowDefinition Height="*"/>
</Grid.RowDefinitions>
Run Code Online (Sandbox Code Playgroud)

提供相同的功能。如果您想要除等高行之外的其他内容,您可以使用:

<Grid.RowDefinitions>
  <RowDefinition Height="1*"/>
  <RowDefinition Height="2*"/>
  <RowDefinition Height="3*"/>
  <RowDefinition Height="4*"/>
</Grid.RowDefinitions>
Run Code Online (Sandbox Code Playgroud)

它将可用高度除以 10 并保持每行的相对高度。或者,这些值可以是 0.1、0.2、0.3 和 0.4 或任何比例值。