ApplicationのResourceにDataGridのStyleを定義して、DataGridTextColumnのDataGridCellを外部から文字列を書き換えられるようにできないでしょうか。
文字列を書き換えるために、DataGridCellのContentPresenter.Content.Textをソースとした双方向(Mode=TwoWay)のBindingを作成して添付プロパティにバインディングしています。
DataGridCellは編集していない時はTextBlock、編集しているときはTextBoxをContentPresenter.ContentがもっているのでおなじPathでそれぞれのTextにBindingできます。
ソース上からBinding.Targetの添付プロパティを書き換えることで、Binding.SourceのTextBox・TextBlockのTextを操作しています。
Cellを編集しているときのTextBoxのTextを書き換えた時はDataGridCellの値も書き換わります。
しかし、Cellを編集していない時のTextBlockのTextは書き換えてもCellの値には影響しません。
TextBlockが文字を表示するときに内部で使用するBinding.ModeがOneWayのため、Textを書き換えても影響しないようです。
このTextBlockのTextとDataGridCellを双方向にBindingできれば、解決するかと思っています。
すべてのDataGridに対して修正を行いため、ApplicationのResourceに修正のためのDataGridのデフォルトStyleを定義して対応したいと考えているのですが、対応方法が思い浮かびません。
試してはいませんが、列ごとのセルのスタイル「DataGridTextColumn.ElementStyle」をつかってContentTemplateを定義すれば双方向BindingのTextBlockを作成できるかもしれません。
ですがDataGridTextColumnは自分自身のStyleをもたないため、Resourceの中でデフォルトStyleを定義することができません。
だからDataGridTextColumn.ElementStyleを使った対応は断念しました。
ApplicationのResourceにDataGridのStyleを定義して、DataGridTextColumnのDataGridCellを外部から文字列を書き換えられるようにできないでしょうか。
私にはResourceにデフォルトのStyleを定義するぐらいしか思いつきませんが、他に解決方法があるのならそちらでもかまいません。
ご教示いただけると幸いです。
データソースを変更して、表示の更新(同期)を行っても表示は変わりませんか?
たしかに各Cellにはデータソース(DataGridTextColumn.Binding)があるので、ソースを直接編集すれば対応は簡単です。
しかし、同様の処理を行いたい箇所は多義にわたるので局所的な対応はしたくありません。
汎用的な処理で吸収したいと考えております。
-xaml
<Style x:Key="DataGridLinkTextStyle" TargetType="DataGridCell"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type DataGridCell}"> <Border x:Name="border" BorderBrush="Transparent" BorderThickness="1" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="auto"/> </Grid.ColumnDefinitions> <ContentPresenter x:Name="presenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> <!--LinkButtonsは内部でLinkButtons.TextにBindingしたソースを書き換えます--> <link:LinkButtons Grid.Column="1" UseType="{Binding Path=Column.(local:DataGridLinkTextColumnStyle.UseType),RelativeSource={RelativeSource AncestorType=DataGridCell,Mode=FindAncestor}}"> <link:LinkButtons.Style> <Style TargetType="link:LinkButtons"> <!--ContentPresenter.Content.TextをソースにしたBindingを作成します--> <Setter Property="Text" Value="{Binding Path=Content.Text,ElementName=presenter}"/> </Style> </link:LinkButtons.Style> </link:LinkButtons> </Grid> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
-xaml.cs
public partial class LinkButtons : ItemsControl { public static readonly System.Windows.DependencyProperty TextProperty = DependencyProperty.Register( nameof(Text), typeof(String), typeof(LinkButtons),new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault) ); /// <summary> /// テキスト /// </summary> public String Text { get => (String)GetValue(TextProperty); set => SetValue(TextProperty, value); } void Hoge() { Text = "代入後、TextBox/TextBlockのTextも変わる。"+ "TextBoxの場合はBindingがTwoWayなのでCellの値も変わり、TextBlockの場合はOneWayなのでCellの値が変わらない"; }
あなたの回答
tips
プレビュー