13

次のような 2 つの textBlocks があります: (.NET FW 3.0 を使用)

<TextBlock Grid.Column="0" Name="tabName" Style="{StaticResource textBlockBarStyle}" HorizontalAlignment="Left">
  <TextBlock.Margin>
      <Binding Converter="{StaticResource dpiConverter}">
          <Binding.ConverterParameter>
               <Thickness Left="3" Top="6" Right="0" Bottom="0"/>
          </Binding.ConverterParameter>
      </Binding>
  </TextBlock.Margin>
</TextBlock>

<TextBox  x:Name="txtBoxHelp" 
          IsReadOnly="True" Style="{DynamicResource txtBoxHelpStyle}" 
          IsTabStop="False" 
          Text="some text" MouseLeftButtonDown="txtBoxHelp_MouseLeftButtonDown">
     <TextBox.Margin>
        <Binding Converter="{StaticResource dpiConverter}">
            <Binding.ConverterParameter>
                 <Thickness Left="7" Top="0" Right="0" Bottom="0"/>
            </Binding.ConverterParameter>
        </Binding>
     </TextBox.Margin>
</TextBox>

これら 2 つの textBlocks は、他の OS ではうまく機能しますが、SP3 を適用した Windows XP Home Version では機能しないことがあります。これらを更新するために多くの方法を試みましたが、失敗しました。

私たちは試しました:

  1. UpdateLayout
  2. InvalidateVisual
  3. コード内の Text プロパティの設定をバインド モードに変更しました。

これらのコントロールを強制的に更新する方法は?

4

4 に答える 4

21

これは、新しいスレッドを作成する必要なく機能します。すべてのバインディングが最初に更新されたときにアクションが開始されるようにスケジュールします。

           Application.Current.Dispatcher.BeginInvoke(
                DispatcherPriority.Background,
                new Action(() =>
                    {
                        // Do something here.
                    }));
于 2011-06-02T15:50:18.840 に答える
5

コントロールを WPF でライブ更新する方法は、TwoWay データバインディングです。そのため、バインド先のすべての viewModel プロパティが Dependency Properties であるか、INotifyPropertyChanged を実装しており (そして正しく処理されているか)、それらの Binding.Mode = TwoWay であることを確認してください。

Rudi GroblerWPF データ バインディングについて私が知らなかった 10 のことを確認してください

いくつかのデータバインディング記事:

  1. WPF データ バインディング - パート 1 Joel Ivory Johnson 著代替テキスト
  2. Josh Smithによる WPF データバインディングへの一歩ずつの移行
于 2009-05-04T12:59:15.563 に答える
4

Thread thread = new Thread(new ThreadStart(delegate()
                {
                    Thread.Sleep(200); // this is important ...
                    try
                    {
                        this.Dispatcher.BeginInvoke(DispatcherPriority.Send,
                            new NoArgsHandle(delegate()
                            {
                               // do something, set .Text = "some text"
                            }));
                    }
                    catch { }
                }));
                thread.Name = "thread-UpdateText";
                thread.Start();

それはうまくいきます。

于 2009-05-15T02:54:17.380 に答える